开发者

flex3 Format date without timezone

I'm receiving a date from a server in milliseconds since 1-1-1970. I then use the DateFormatter to print the date to the screen. However, Flex adds time开发者_如何学Cdifference and thus it displays a different time than what I got from the server. I've fixed this by changing the date before printing to screen. But I think that's a bad solution because the date object doesn't hold the correct date.

Does anyone know how to use the dateFormatter to print the date, ignoring the timezone?

this is how I did it:

function getDateString(value:Date):String
{
    var millisecondsPerMinute:int = 1000*60;
    var newDate:Date = new Date(value.time - (millisecondsPerMinute*value.timezoneOffset));

    var dateFormatter:DateFormatter = new DateFormatter();
    dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";

    return dateFormatter.format(newDate);
}


Maybe there is something I'm missing but this seems to work for me.

<?xml version="1.0"?>
<!-- formatters\FormatterDateField.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<!-- Declare a DateFormatter and define formatting parameters.-->
<mx:DateFormatter id="dateFormatter" 
    formatString="EEEE DD-MM-YYYY LL:NN:SS AA"/>

<mx:Label text="Millis (1220836618601 == Monday 08-09-2008 01:16:58 AM):"/>
<mx:TextInput id="dob" text="1220836618601"/>

<mx:Label text="Formatted date UTC: "/>
<mx:TextInput id="formattedDate" 
    text="" 
    editable="false"/>
<mx:Label text="Formatted date local: "/>
<mx:TextInput id="formattedDateLoc" 
    text="" 
    editable="false"/>

<!-- Format and update the date.-->
<mx:Button label="Format Input" 
    click="
        var d :Date = new Date(parseInt(dob.text));
        formattedDate.text=dateFormatter.format(d.toUTCString());
        formattedDateLoc.text=dateFormatter.format(d);
    "/>
</mx:Application>

Suggesting that instead of passing the date object (which is timezone dependant) into the dateFormatter, pass in the date object's UTC String instead. I didn't find anything that would suggest that the DateFormatter does anything to the timezone, so there shouldn't be any need to try to compensate for the timezone, especially when the date object already provides a method for getting the UTC.

function getDateString(value:Date):String
{
    var dateFormatter:DateFormatter = new DateFormatter();
    dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";

    return dateFormatter.format(value.toUTCString());
}


In Flex Hero 4.5 you can use the new Spark DateTimeFormatter:

<s:DateTimeFormatter dateTimePattern="HH':'mm':'ss" id="dateFormatterUTC" useUTC="true" />
<s:Label text="{dateFormatterUTC.format(new Date())}" />


The most simple of fixes is to have as many objects as you can (and properties of objects) be strings. The timezoneOffset solution works fine, but the timezoneOffset for many US cities changes twice during the year. The best rule -- everything is a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜