测试了下as3的日期时间格式化

星期日, 2011-01-23 | Author: Lee | as | 6,563 views

在java中格式化日期很方便,在as3中找不到对应的函数,自己写个吧,也很方便,简单用flex测试了下
如此的日期格式:2011-01-23 00:36:31
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" initialize="init()">
	<fx:Declarations>
		<!--power by I5A6 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import mx.formatters.DateFormatter;
			private function init():void{
				timeBut.addEventListener(MouseEvent.CLICK,handleClick);
			}
			private function handleClick(event:MouseEvent):void{
				var date:Date=new Date();
				var  dateStr:String=getDateString(date);
				lable.text+=dateStr+"\n";
				trace(dateStr);
			}
 
			static public function getDateString(date:Date):String{
				var date:Date=new Date();
				var year:String=String(date.getFullYear());
				var month:String=String((date.getMonth()+1)<10?"0":"")+(date.getMonth()+1);
				var day:String=String(date.getDate()<10?"0":"")+(date.getDate());
				var hours:String=String(date.getHours()<10?"0":"")+date.getHours();
				var min:String=String(date.getMinutes()<10?"0":"")+date.getMinutes();
				var second:String=String(date.getSeconds()<10?"0":"")+date.getSeconds();
 
				return year+"-"+month+"-"+day+" "+hours+":"+min+":"+second;
			}
		]]>
	</fx:Script>
	<s:VGroup width="100%">
		<s:Button id="timeBut" label="获取当前时间" />
		<s:Label id="lable"/>
	</s:VGroup>
</s:Application>

Tags: , ,

文章作者: Lee

本文地址: https://www.pomelolee.com/727.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Meta