Search code examples
apache-flexactionscript-3datedayofweek

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?

Thanks!!


Solution

  • 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