开发者

Find date for same day of the week last year?

OK so for example, today is Tuesday, Feb 02. Well the equivalent "Tuesday" from last year was on Feb 03.

How can I find this out programmatically开发者_Python百科?

Thanks!!


Just my two cents. I don't like the idea, that the second answer assumes 52 weeks in a year, it will work for a single year, but is a solution to only this exact problem - eg. if you want to check the same thing moving back 10 years it won't work. I'd do it like this:

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);

returns:

Tue Feb 2 21:13:18 GMT+0100 2010 
Tue Feb 3 21:13:18 GMT+0100 2009


According to Google, there are 604,800,000 milliseconds in a week. That times 52 should give you the same day of the week a year later (right?).

For example:

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

Output:

Tue Feb 2 00:00:00 GMT-0800 2010
Tue Feb 3 00:00:00 GMT-0800 2009
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜