Flex中如何利用String类的replace()和Date.parse()函数对ISO格式日期进行解析的例子
By Minidxer | March 19, 2009
接下来的例子演示了Flex中如何利用String类的replace()和Date.parse()函数,对ISO格式日期进行解析。关于ISO格式日期请参考:http://www.w3.org/TR/NOTE-datetime
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white"
- creationComplete="init();">
- <mx:Script>
- <![CDATA[
- [Bindable]
- private var d:Date;
- private function init():void {
- var isoStr:String = "2008-01-25T06:14:23Z";
- d = isoToDate(isoStr);
- formHeading.label = isoStr;
- }
- private 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));
- }
- ]]>
- </mx:Script>
- <mx:Form>
- <mx:FormHeading id="formHeading" />
- <mx:FormItem label="toString():">
- <mx:Label text="{d.toString()}" />
- </mx:FormItem>
- <mx:FormItem label="toLocaleString():">
- <mx:Label text="{d.toLocaleString()}" />
- </mx:FormItem>
- <mx:FormItem label="toUTCString():">
- <mx:Label text="{d.toUTCString()}" />
- </mx:FormItem>
- </mx:Form>
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Other |
No Comments » |
Tags: Date, parse(), replace(), toLocaleString(), toUTCString()