3

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!!

JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
  • 2
    You may need to define what an "equivalent tuesday" is, is it the nearest tuesday to Feb 2? is it that they are both the first tuesday of the month? or the 5th tuesday of the year? The answers to these may usually be the same but might not be in all cases. – invertedSpear Feb 02 '10 at 18:57
  • To find the same weekday one year ago, simply subtract 52 weeks or 364 days from the given date. See http://stackoverflow.com/a/41776747/3817004. – Uwe Jan 21 '17 at 08:03

2 Answers2

4

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
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
Stiggler
  • 2,800
  • 20
  • 21
  • I think you may have meant to say 604 800 000ms in a *week* not a year. – Dan Feb 02 '10 at 18:58
  • Yikes, it was a simple typo people; I obviously meant week. – Stiggler Feb 02 '10 at 19:02
  • I like this nice concise answer although question whether the calculation for 7*24*60*60*1000 needed looking up in google! – spender Feb 02 '10 at 19:15
  • @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 Feb 02 '10 at 19:21
  • Downvote was due to a mis-read, sorry. It should be undone now. – GreenMatt Feb 02 '10 at 19:25
  • Revise that last comment: I tried to undo it, but have been told it's too late unless you edit the answer again – GreenMatt Feb 02 '10 at 19:32
3

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
  • 4,246
  • 19
  • 20
  • 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 Feb 02 '10 at 20:28
  • 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 Feb 02 '10 at 20:44