Avg. Rating 5.0

Problem

I want to run a count down timer that run reverse from next Monday. two date i used. 1. current Date() 2. next Monday date. How i find the next Monday date whenever i run my app according to current time/date ? Thanks.

Solution

Using the Date.day property calculate the difference between now and the next Monday.

Detailed explanation

I have commented the code below and I think it explains it sufficiently

//Get todays date
var now:Date = new Date();
//Set the time to midnight
now.setHours(0,0,0,0);
//Get the day of the week Sun = 0, Mon = 1, etc...
var day:int = now.getDay();
//Will be used to calculate next monday
var dayDifference:int = 0;
//Sunday is the exception to catch
if (day == 0) {
    dayDifference = 1;
}
else {
    //Every other day the difference can be calculated as 8 - day of the week
    dayDifference = 8 - day;
}
//Use the timestamp to setup the next monday date
var nextMonday:Date = new Date(now.time);
//Add the number of days to get next monday
nextMonday.date += dayDifference;

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes