如何在Flex(AS3)中解析ISO格式的日期?

[英]How to parse an ISO formatted date in Flex (AS3)?


How can I parse an ISO date string into a date object in Flex (AS3)?

如何在Flex(AS3)中將ISO日期字符串解析為日期對象?

e.g.
2009-12-08T04:23:23Z
2009-12-08T04:23:23.342-04:00
etc...

例如2009-12-08T04:23:23Z 2009-12-08T04:23:23.342-04:00等......

3 个解决方案

#1


import com.adobe.utils.DateUtil;

var dateString:String = "2009-03-27T16:28:22.540-04:00";
var d:Date = DateUtil.parseW3CDTF(dateString);
trace(d);
var s:String = DateUtil.toW3CDTF(d);
trace(s);
[trace] Fri Mar 27 16:28:22 GMT-0400 2009
[trace] 2009-03-27T20:28:22-00:00

Turns out DateUtil handles everything in the W3C Date and Time spec. AS3 Dates do not maintain milliseconds, but they'll just be dropped if available.

原來DateUtil處理W3C日期和時間規范中的所有內容。 AS3日期不會保持毫秒數,但如果可用,它們將被刪除。

Note that the W3C output is converted to UTC (aka GMT, or Zulu time).

請注意,W3C輸出轉換為UTC(也稱為GMT或Zulu時間)。

#2


Example function to convert ISO into Date format

將ISO轉換為日期格式的示例函數

    public function isoToDate(value:String):Date 
    {
        var dateStr:String = value;
        dateStr = dateStr.replace(/\-/g, "/");
        dateStr = dateStr.replace("T", " ");
        dateStr = dateStr.replace("Z", " GMT-0000");

        return new Date(Date.parse(dateStr));
    }

#3


Here is an implementation: http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/

這是一個實現:http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/

(Sorry ff just isn't showing the linking button and I am too lazy to do it myself.)

(對不起,如果沒有顯示鏈接按鈕,我自己也懶得去。)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/03/27/72f9dbcb4e7321982664cb6fcf95f44c.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com