A few date manipulation functions for JS/AS…
Convert a MySQL DATE or DATETIME field into a valid JS/AS Date object:
function fromMySQL(date) { date = date.replace(/^(\d{4})-(\d{2})-(\d{2})/g, '$2-$3-$1'); return new Date(date); };
UPDATE: Apparently FireFox can’t handle dashes when parsing the date. In the above replacement, use slashes instead:
// updated to play nice with Fx function fromMySQL(date) { date = date.replace(/^(\d{4})-(\d{2})-(\d{2})/g, '$2/$3/$1'); return new Date(date); };
Convert a JS Date Object to a MySQL formatted string
function JSDate2MySQLDate = function(date){ var year = date.getFullYear(); var month = ('0' + (date.getMonth() + 1)).slice(-2); var day = ('0' + date.getDate()).slice(-2); return [year, month, day].join('-'); }
Get amount of time between two dates – returns object with milliseconds, seconds, minutes, hours, days and weeks:
function timeBetween(from, to) { var diff = Math.abs(Math.floor(Number(from) - Number(to))); return { 'milliseconds' : diff, 'seconds' : diff / 1000, 'minutes' : diff / 60000, 'hours' : diff / 360000, 'days' : diff / 86400000, 'weeks' : diff / 604800000 } }
Return true/false if a given date falls on a weekend:
function isWeekend(date) { return Boolean(date.getDay() % 6); }
Takes a given date and adds 1 day at a time until the date falls on a weekend. The second argument determines if the date should move backward (true) or backward (false, default):
function moveToWeekend(date, decrease) { var day = date.getDate(); var increment = decrease ? -1 : 1; while (date.getDay() % 6 > 1) date.setDate(day += increment); return date; }
Takes a given date and adds 1 day at a time until the date falls on a weekday. The second argument determines if the date should move backward (true) or backward (false, default):
function moveOffWeekend(date, decrease) { var day = date.getDate(); var increment = decrease ? -1 : 1; while (date.getDay() % 6 == 1) date.setDate(day += increment); return date; }
All bundled up in a DateTools static utility class:
[as3]
package {
public class DateTools {
public function DateTools():void {
throw new Error("DateTools should not be instantiated.");
}
public static const MONTH_LABELS:Array = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
public static const DAY_LABELS:Array = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
];
public static function fromMySQL(date:String):Date {
date = date.replace(/^(\d{4})-(\d{2})-(\d{2})/g, ‘$2-$3-$1’);
return new Date(date);
}
public static function timeBetween(from:Date, to:Date):Object {
var diff:uint = Math.abs(Math.floor(Number(from) – Number(to)));
return {
‘milliseconds’ : diff,
‘seconds’ : diff / 1000,
‘minutes’ : diff / 60000,
‘hours’ : diff / 360000,
‘days’ : diff / 86400000,
‘weeks’ : diff / 604800000
}
}
public static function isWeekend(date:Date):Boolean {
return Boolean(date.getDay() % 6);
}
public static function moveToWeekend(date:Date, increase:Boolean = true):Date {
var day:Number = date.getDate();
var increment:int = increase ? 1 : -1;
while (date.getDay() % 6 > 1) date.setDate(day += increment);
return date;
}
public static function moveOffWeekend(date:Date, increase:Boolean = true):Date {
var day:Number = date.getDate();
var increment:int = increase ? 1 : -1;
while (date.getDay() % 6 == 1) date.setDate(day += increment);
return date;
}
}
}
[/as3]
public static function isWeekend(date:Date):Boolean { return Boolean(date.getDay() % 6); }
Boolean( 0 % 6 ) = false
Boolean( 1 % 6 ) = true
Boolean( 2 % 6 ) = true
Boolean( 3 % 6 ) = true
Boolean( 4 % 6 ) = true
Boolean( 5 % 6 ) = true
Boolean( 6 % 6 ) = false
If you want to test whether date.getDay() is 5 or 6 (Saturday or Sunday, according to DAY_LABELS):
return date.getDay() >= 5;
moveToWeekend() and moveOffWeekend() have similar issues
Thanks for the comment. Sunday is 0 and Saturday is 6, so actually the modulus test is correct, but backwards… Should be !(date.getDay() % 6); or (date.getDay() % 6) == 0
function isWeekend(date) {
return !(date.getDay() % 6);
}
for(var i=12;i<20;i++){
var date = new Date("Jan " + i +", 2012");
console.log(date + " is weekend = " + isWeekend(date));
}
DAY_LABELS is incorrect in this context (and apparently not used... must have made it over in a paste from a larger class).
I'll try to update the post when I get a minute (unless some kind stranger wants to do it for me)...