简体   繁体   English

flex3格式化不带时区的日期

[英]flex3 Format date without timezone

I'm receiving a date from a server in milliseconds since 1-1-1970. 我从1970年1月1日开始以毫秒为单位从服务器接收日期。 I then use the DateFormatter to print the date to the screen. 然后,我使用DateFormatter将日期打印到屏幕上。 However, Flex adds timedifference and thus it displays a different time than what I got from the server. 但是,Flex增加了时差,因此显示的时间与我从服务器获得的时间不同。 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. 但是我认为这是一个不好的解决方案,因为date对象没有正确的日期。

Does anyone know how to use the dateFormatter to print the date, ignoring the timezone? 有谁知道如何使用dateFormatter来打印日期,而忽略时区?

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. 建议不要将日期对象(取决于时区)传递给dateFormatter,而应传递日期对象的UTC字符串。 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. 我没有发现任何建议DateFormatter对时区执行任何操作的内容,因此不需要尝试补偿时区,特别是当date对象已经提供了获取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 : 在Flex Hero 4.5中,您可以使用新的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. timezoneOffset解决方案可以正常工作,但是美国许多城市的timezoneOffset在这一年中发生了两次更改。 The best rule -- everything is a string. 最佳规则-一切都是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM