tags:

views:

101

answers:

1

Hello to all!

I get a Date of my database and I need to show it as a String. So in Flex I do this:

public static function dateToString(cDate:Date):String {
        return cDate.date.toString()+"."+
            cDate.month.toString()+"."+
            cDate.fullYear.toString()+" "+
            cDate.hours.toString()+":"+
            cDate.minutes.toString()+":"+
            cDate.seconds.toString();
}

But I get for example the result:

13.7.2010 0:0:15

How can I fill the day, month, hours, minutes, seconds with padded 0?

And, I go back from String to Date with:

DateField.stringToDate(myTextInput.text, "DD.MM.YYYY HH:MM:SS");

Is this correct? I want to have a Date which I will transfer via BlazeDS to a J2EE Backend, but I only see in the database then a null value. So something is going wrong...

Best Regards.

A: 

Have you seen the DateFormatter class?

Example:

import mx.formatters.DateFormatter;

private var dateFormatter:DateFormatter;

private function init():void
{
    dateFormatter = new DateFormatter();
    dateFormatter.formatString = 'DD.MM.YYYY HH:NN:SS'
}

public function dateToString(d:Date):String
{
    return dateFormatter.format(d);
}

public function stringToDate(s:String):Date
{
    return dateFormatter.parseDateString(s);
}

It looks like somebody was asleep the day the wrote Flex 3.2, because DateFormatter::parseDateString is a protected function. It looks like they fixed that by 3.5.

Ryan Lynch
The method stringToDate does not work, because it is a protected method. I am using Flex 4.
Tim