PDA

View Full Version : Simple Date comparison question


rattleheadmegafan
04-07-2008, 08:30 PM
Hi all,

I have to read a date from a textbox which is typically like this 2008/04/07 15:30:43

I want to be able to read this date and compare it with current date (which I have stored in a variable). Could anyone tell me how to convert String to Date in Flex?

I tried doing var d1: Date = Date(mylbl.text); but it didn't seem to work.

Thanks,
Sharat

lemonizer
04-08-2008, 12:42 AM
I glanced over the Date() doc here: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Date.html#Date()

here's some valid string formats for the Date class:

The Date() constructor takes up to seven parameters (year, month, ..., millisecond) to specify a date and time to the millisecond. The date that the newly constructed Date object contains depends on the number, and data type, of arguments passed.

* If you pass no arguments, the Date object is assigned the current date and time.
* If you pass one argument of data type Number, the Date object is assigned a time value based on the number of milliseconds since January 1, 1970 0:00:000 GMT, as specified by the lone argument.
* If you pass one argument of data type String, and the string contains a valid date, the Date object contains a time value based on that date.
* If you pass two or more arguments, the Date object is assigned a time value based on the argument values passed, which represent the date's year, month, date, hour, minute, second, and milliseconds.

If you pass a string to the Date class constructor, the date can be in a variety of formats, but must at least include the month, date, and year. For example, Feb 1 2005 is valid, but Feb 2005 is not. The following list indicates some of the valid formats:

* Day Month Date Hours:Minutes:Seconds GMT Year (for instance, "Tue Feb 1 00:00:00 GMT-0800 2005", which matches toString())
* Day Month Date Year Hours:Minutes:Seconds AM/PM (for instance, "Tue Feb 1 2005 12:00:00 AM", which matches toLocaleString())
* Day Month Date Year (for instance, "Tue Feb 1 2005", which matches toDateString())
* Month/Day/Year (for instance, "02/01/2005")


You may have to add some logic to convert the month into logical names ("Jan", "Feb", etc) before you convert it to a Date.

kahuja
04-08-2008, 08:46 AM
Try the "Corelib (http://labs.adobe.com/wiki/index.php/ActionScript_3:resources:apis:libraries)" library that has some date parsing functions.

dr_zeus
04-08-2008, 05:50 PM
Better link for corelib:
http://code.google.com/p/as3corelib/