Reputation: 165
I am taking date input from DateField in flex. And I have an XML file with date in YYYY/MM/DD format i.e string format. How to convert this string to Date object? And I have to compare these two values?
I am getting "Tue Feb 4 00:00:00 GMT-0800 2014" this type of output from the DateField selection. And I have to use "dateCompare()" function(It's like restriction).
Upvotes: 0
Views: 2900
Reputation: 1394
Date.parse function should work with that format , if is not working you can use the Array.split to get the values you need,
var d:Date=new Date();
var matches:Array=string.split("/");
d.setUTCFullYear(int(matches[0]), int(matches[1]) - 1, int(matches[2]))
To Set the time you can use the method setUTCHours
d.setUTCHours(0,0,0,0)
EDIT:
var string:String="2008/11/13";
var d:Date=new Date();
var matches:Array=string.split("/");
d.setUTCFullYear(int(matches[0]), int(matches[1]) - 1, int(matches[2]))
trace(d);
d.setUTCHours(12,11,13);
trace(d);
Upvotes: 3
Reputation: 1615
The simpler way is to use the parseDateStrig static method from mx.formatters.DateFormatter
var date:Date = DateFormatter.parseDateString("2013/1/11", "YYYY/MM/DD");
Upvotes: 1
Reputation: 26
Use DateTimeFormatter... for specific format....then check with your xml data.
s:DateTimeFormatter id="datePattern" dateTimePattern="yyyy/MM/dd"
if(datePattern.format(dateChooser.selectedDate) == "2014/03/01")
lblCheck.text = "Check...";
else
lblCheck.text = "Not Check..";
Use below link for DateTimeFormatter...
http://help.adobe.com/en_US/flex/using/WS8b1c39bd7e9fc364-70b5c6d212c98ca85e2-8000.html
Upvotes: 0
Reputation: 10469
This is a function I use to do something similar. You can adapt it to fit your needs.
public static function convertSQLDate( dateString : String ) : Date
{
if( dateString == null ) return null;
var datePattern : RegExp = /(\d{4})-(\d+)-(\d+)( (\d+):(\d+):(\d+))?/;
var result : Object = datePattern.exec( dateString );
if( result[ 4 ] != null ) return new Date( result[1], result[2] - 1, result[3], result[ 5 ], result[ 6 ], result[ 7 ] );
else return new Date( result[1], result[2] - 1, result[3] );
}
Upvotes: 0