Pixelbox

Welcome to Pixelbox. Friday the 10 of October 2008

Skip to content >>

Date conversions in AS3

I have been re factoring some stuff in action script 3 recently and the old date manipulation was a bit hacky. It included get time then adding multiples of milliseconds to epocs, which was a bit confusing.

I though I would be a good idea to write a few methods in my personal utilities class, simple stuff like returning yesterday, tomorrow or this time next week etc. After a bit of research I found that some time constants have already been defined in the Action Script 3.0 cookbook utils. If you don't have it then these are the constants for various amounts of time.


	public static const MILLISECOND:Number = 1;
	public static const SECOND:Number = MILLISECOND * 1000;
	public static const MINUTE:Number = SECOND * 60;
	public static const HOUR:Number = MINUTE * 60;
	public static const DAY:Number = HOUR * 24;
	public static const WEEK:Number = DAY * 7;

With these handy chaps you can easily get a date variable for yesterday or tomorrow or in 7 days:


	//Replace DateUtilities. with this. if you are including them in your own class

	var yesterday:Date = new Date();
	yesterday.time -= DateUtilities.DAY;

	var tomorrow:Date = new Date();
	tomorrow.time += DateUtilities.DAY;

	var weekToday:Date = new Date();
	weekToday.time += DateUtilities.WEEK;

Neat hu!

There are no comments yet

Add your own comment