views:

102

answers:

2

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?

Thanks!!

+2  A: 
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
Stiggler
I think you may have meant to say 604 800 000ms in a *week* not a year.
Dan
Yikes, it was a simple typo people; I obviously meant week.
Stiggler
definitely not enough of a typo for a downvote.
invertedSpear
I like this nice concise answer although question whether the calculation for 7*24*60*60*1000 needed looking up in google!
spender
@spender: It was the quickest way :)@invertedSpear: Whomever it was initially left a comment expressing his/her dissatisfaction with the typo, then immediately withdrew it while leaving the downvote.
Stiggler
Downvote was due to a mis-read, sorry. It should be undone now.
GreenMatt
Revise that last comment: I tried to undo it, but have been told it's too late unless you edit the answer again
GreenMatt
+2  A: 

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
Robert Bak
If you're going to say that, you might want to consider the leap seconds..
Timmy
Sorry? I don't think I understand what you mean. 52 weeks equals 364 days. Which means that what you actually do is move the date forward a day or two. Here you actually use the actionscript date class to move back, and than look around the date it gives you.
Robert Bak
Agreed, your answer looks to be cleaner than either mine or Stiggler's. In fact, I just realized my answer doesn't actually answer the original question...
Dan