3

好的,例如,今天是 2 月 2 日,星期二。去年的“星期二”是 2 月 3 日。

我怎样才能以编程方式找到它?

谢谢!!

4

2 回答 2

3

根据谷歌的数据,一周有 604,800,000 毫秒。那次 52 应该给你一年后一周中的同一天(对吗?)。

例如:

var date:Date = new Date(2010, 1, 2);
trace(date);
date.setMilliseconds(date.milliseconds - 604800000 * 52);
trace(date);

输出:

Tue Feb 2 00:00:00 GMT-0800 2010
Tue Feb 3 00:00:00 GMT-0800 2009
于 2010-02-02T18:52:05.400 回答
3

只是我的两分钱。我不喜欢这个想法,第二个答案假设一年有 52 周,它将工作一年,但它只能解决这个确切的问题 - 例如。如果您想检查 10 年后的同一件事,那将是行不通的。我会这样做:

var today:Date = new Date();    
// Here we store the day of the week            
var currentDay:int = today.day;
trace (today);      
const milisecondsInADay:uint = 1000*60*60*24;
// Here we move back a year, but we can just as well move back 10 years
// or 2 months      
today.fullYear -= 1;
// Find the closest date that is the same day of the week as current day
today.time -= milisecondsInADay*(today.day-currentDay);     
trace (today);

返回:

Tue Feb 2 21:13:18 GMT+0100 2010 
Tue Feb 3 21:13:18 GMT+0100 2009
于 2010-02-02T20:15:02.137 回答