2009-12-14 48 views
0

我收到一個服務器的日期,以毫秒爲單位,自1-1-1970年。然後我使用DateFormatter將日期打印到屏幕上。但是,Flex添加了時間差異,因此顯示的時間與我從服務器獲得的時間不同。我通過在打印到屏幕之前更改日期來解決此問題。但我認爲這是一個不好的解決方案,因爲日期對象不包含正確的日期。flex3格式日期不帶時區

有誰知道如何使用dateFormatter打印日期,忽略時區?

我這是怎麼做的:

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); 
} 
+0

我沒有嘗試這樣做,我不知道這是否會幫助,但你嘗試使用日期對象UTC的功能呢? – 2009-12-14 11:31:14

+0

嗯,它實際上是創建字符串的DateFormatter,而DateFormatter調用常規函數,因此不會執行任何操作。我也嘗試過'newDate.timezoneOffset = 0',但timezoneOffset是隻讀的,所以也不起作用。 – 2009-12-14 12:20:04

回答

2

也許有我丟失的東西,但是這似乎爲我工作。

<?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> 

。暗示,而不是使所述對象日期(這是時區相關的)到dateFormatter,通過在日期對象的UTC字符串來代替。我沒有發現任何暗示DateFormatter會對時區做任何事情的東西,因此不應該有任何需要嘗試補償時區,尤其是當日期對象已經提供了獲取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()); 
} 
+0

它似乎只與-locale一起工作en_US – 2011-04-11 09:44:55

-1

最簡單的修復方法是儘可能多的對象(和對象的屬性)是字符串。 timezoneOffset解決方案正常工作,但美國許多城市的timezoneOffset在這一年中會發生兩次變化。最好的規則 - 一切都是一個字符串。

+0

整個歐洲也每年更換兩次時區。我不明白這是一個問題。最好你希望你的程序知道這一點,而不是堅持使用錯誤的時區。我同意用手抵消時區並不是最好的解決辦法,但也不是無視這些信息。由於系統中有日期功能,請學習如何使用它! – 2009-12-16 09:43:55

+0

嘿馬蒂。我會告訴你爲什麼項目架構師在使用AMF數據時通常選擇使用字符串(這是問題的第一個問題):如果您從紐約服務器向芝加哥的swf發送一個日期對象(AMF序列化)(無論服務器語言),2009年12月16日00:00:00在紐約的DateObject「toLocalString()跟蹤」爲12/15/2009 23:00:00 PM在芝加哥的用戶。這就是AMF的本質......這就是爲什麼所有結構良好的主要數據密集型項目都使用AMF序列化數據的字符串。 – jeremym 2009-12-16 17:04:10

1

在Flex英雄4.5,你可以使用新的Spark DateTimeFormatter

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