首页 > 代码库 > As3 常用日期工具

As3 常用日期工具

?
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.lj.utils  
{  
    import mx.controls.DateField;  
    import mx.controls.dataGridClasses.DataGridColumn;  
    import mx.formatters.DateFormatter;  
   
    /** 
     * <p>Title:日期时间工具类 </p> 
     * <p>Description:提供时间日期的一些to string 或  string to 的操作</p> 
     * <p>Copyright: 个人所有</p> 
     * <p>Company: 刘军</p> 
     * @author 刘军 TomLiu 
     * @version 1.0 LastUpdata: 2012-12-29 
     */ 
    public class DateTimeUtils  
    {  
   
        public static const MILLISECOND:Number=1;  
        public static const SECOND:Number=MILLISECOND * 1000;  
        public static const MINUTE:Number=SECOND * 60;  
        public static const HOUR:Number=MINUTE * 60;  
        public static const DAY:Number=HOUR * 24;  
        public static const WEEK:Number=DAY * 7;  
        public static const CHINESE_DATE_FORMAT:String="YYYY-MM-DD";  
        public static const CHINESE_DATETIME_FORMAT:String="YYYY-MM-DD";  
   
   
   
        /** 
         * 字符串转换成日期类型(使用mx.controls.DateField类) 
         * @param dateString 
         * @param formatString 
         * @return  
         */ 
        public static function stringToDate(dateString:String, formatString:String=CHINESE_DATE_FORMAT):Date  
        {  
            return DateField.stringToDate(dateString,formatString);  
        }  
            
        /** 
         * 日期转换成字符串类型(使用mx.controls.DateField类) 
         * @param date 
         * @param formatString 
         * @return  
         */ 
        public static function dateToString(date:Date,formatString:String=CHINESE_DATE_FORMAT):String{  
            return DateField.dateToString(date,formatString);  
        }  
   
        /** 
         * 格式化时间YYYY-MM-DD 
         * @param date 
         * @param formatString 
         * @return 
         */ 
        public static function formatDateTime(date:Date, formatString:String=CHINESE_DATE_FORMAT):String  
        {  
   
            //需要as3corelib.swc如果没有可以直接使用 :return dateToString(data,formatString);  
            var dateFormater:DateFormatter=new DateFormatter();  
            dateFormater.formatString=formatString;  
            return dateFormater.format(date);  
        }  
   
        /** 
         * 格式化时间YYYY-MM-DD HH:NN:SS 
         * @param date 
         * @param formatString 
         * @return 
         */ 
        public static function formatFullDateTime(date:Date, formatString:String=CHINESE_DATETIME_FORMAT):String  
        {  
            //需要as3corelib.swc如果没有可以直接使用 :return dateToString(data,formatString);  
            var dateFormater:DateFormatter=new DateFormatter();  
            dateFormater.formatString=formatString;  
            return dateFormater.format(date);  
        }  
   
        /** 
         * 为DataGridColumn提供时间格式化的labelFunction 
         
         * @param item 
         * @param column 
         * @return 
         
         */ 
        public static function formatDateForDataGridColumn(item:Object, column:DataGridColumn):String  
        {  
   
            return formatDateTime(item[column.dataField], CHINESE_DATE_FORMAT);  
        }  
   
        /** 
         * 为DataGridColumn提供时间格式化的labelFunction 
         
         * @param item 
         * @param column 
         * @return 
         
         */ 
        public static function formatDateTimeForDataGridColumn(item:Object, column:DataGridColumn):String  
        {  
   
            return formatDateTime(item[column.dataField], CHINESE_DATETIME_FORMAT);  
        }  
   
        /** 
         * 给制定时间添加天数或减少天数 
         * @param date 
         * @param addDay 
         * @return 
         
         */ 
        public static function addDaysByDateTime(date:Date, addDay:Number):Date  
        {  
            return new Date(date.getTime() + addDay * DAY);  
        }  
            
        /** 
         * 取下一天 
         */ 
        public static function  getNextDay(currentDate:Date):Date{  
            return addDaysByDateTime(currentDate,1);  
        }  
            
        /** 
         * 取上一天 
         */ 
        public static function  getLastDay(currentDate:Date):Date{  
            return addDaysByDateTime(currentDate,-1);  
        }  
   
        /** 
         * 取下一个月 
         */ 
        public static function getNextMonth(currentDate:Date):Date  
        {  
            var returnDate:Date=new Date(currentDate.getTime());  
            returnDate.setMonth(returnDate.getMonth() + 1, returnDate.getDate());  
            return returnDate;  
        }  
            
        /** 
         * 取上一个月 
         */ 
        public static function getLastMonth(currentDate:Date):Date  
        {  
            var returnDate:Date=new Date(currentDate.getTime());  
            returnDate.setMonth(returnDate.getMonth() - 1, returnDate.getDate());  
            return returnDate;  
        }  
   
        /** 
         * 取下一个年 
         */ 
        public static function getNextYear(currentDate:Date):Date  
        {  
            var returnDate:Date=new Date(currentDate.getTime());  
            returnDate.setFullYear(returnDate.getFullYear() + 1);  
            return returnDate;  
        }  
   
        /** 
         * 取上一个年 
         */ 
        public static function getLastYear(currentDate:Date):Date  
        {  
            var returnDate:Date=new Date(currentDate.getTime());  
            returnDate.setFullYear(returnDate.getFullYear() - 1);  
            return returnDate;  
        }  
   
   
        /** 
         * 取当月月底 
         */ 
        public static function getFristDayOfMonth(currentDate:Date):Date  
        {  
            currentDate.setMonth(currentDate.getMonth(), 1); //下个月的第一天,也就是下个月1号  
            return currentDate;  
        }  
            
        /** 
         * 取当月月底 
         */ 
        public static function getLastDayOfMonth(currentDate:Date):Date  
        {  
            currentDate.setMonth(currentDate.getMonth() + 1, 1); //下个月的第一天,也就是下个月1号  
            currentDate.setDate(currentDate.getDate() - 1); //下个月1号之前1天,也就是本月月底  
            return currentDate;  
        }  
            
   
        /** 
         * 获取日期的中文表示方式:例如星期一\星期二之类的。(注意0表示星期天) 
         * @param currentDate 
         * @return 
         */ 
        public static function getChineseDay(currentDate:Date):String  
        {  
            switch (currentDate.getDay())  
            {  
                case 0:  
                    return "星期日";  
   
                case 1:  
                    return "星期一";  
   
                case 2:  
                    return "星期二";  
   
                case 3:  
                    return "星期三";  
   
                case 4:  
                    return "星期四";  
   
                case 5:  
                    return "星期五";  
   
                case 6:  
                    return "星期六";  
   
                default:  
                    return "";  
            }  
        }  
   
        /** 
         * 获取日期的中文表示方式:例如星期一\星期二之类的。(注意0表示星期天) 
         * @param currentDate 
         * @return 
         */ 
        public static function getEnglishDay(currentDate:Date):String  
        {  
            switch (currentDate.getDay())  
            {  
                case 0:  
                    return "Sunday";  
   
                case 1:  
                    return "Monday";  
   
                case 2:  
                    return "Tuesday";  
   
                case 3:  
                    return "Wednesday";  
   
                case 4:  
                    return "Thursday";  
   
                case 5:  
                    return "Friday";  
   
                case 6:  
                    return "Saturday";  
   
                default:  
                    return "";  
            }  
        }  
   
   
    }