Pisti
Pisti

Reputation: 133

Getting date as string - need to convert

Programming in Flex 4.5

I'm getting a date as a String.

I don't know what date or hour I'm getting.

I want to convert the string to date and take only the hours & minutes.

For example:

Getting - "2012-02-07T13:35:46+02:00"

I want to see: 13:35.

Suggestions or any other solutions?

After some digging, Solution:

var myDate:Date;

myDate = DateFormmater.parseDateString(myDateString);

var dateResult:String = myDate.getHours() + ":" + myDate.getMinutes();

Thanks anyway! :-)!

Upvotes: 0

Views: 1610

Answers (3)

pho
pho

Reputation: 25490

I see you've already got the answer, but for future users, here it is.

var myDateString:String="2012-02-07T13:35:46+02:00"

//This is of the format <yyyy-mm-dd>T<hh:mm:ss><UTC-OFFSET AS hh:mm>
//You could write your own function to parse it, or use Flex's DateFormatter class

var myDate:Date=DateFormatter.parseDateString(myDateString);

//Now, myDate has the date as a Flex Date type.
//You can use the various date functions. In this case,

trace(myDate.getHours()); //Traces the hh value
trace(myDate.getMinutes()); //Traces the mm value

Upvotes: 0

rejo
rejo

Reputation: 3350

private function init():void  
{           
    var isoStr:String = "2012-02-07T13:35:46+02:00";         
    var d:Date = new Date;       
    d = isoToDate(isoStr)    
    trace(d.hours);  
}

private function isoToDate(value:String):Date  
{   
    var dateStr:String = value;   
    dateStr = dateStr.replace(/\-/g, "/");    
    dateStr = dateStr.replace("T", " ");     
    dateStr = dateStr.replace("+02:00", " GMT-0000");   
    return new Date(Date.parse(dateStr));   
}

Upvotes: 0

Korhan Ozturk
Korhan Ozturk

Reputation: 11320

You can to use date.getHours() and date.getMinutes(). Try the following:

var d:Date = DateField.stringToDate("your_date_string","YYYY-MM-DD");
trace("hours: ", date.getHours()); // returns 13
trace("minutes: ", date.getMinutes()); // returns 35

Upvotes: 1

Related Questions