FLEX日期格式化處理和字串轉換

2021-06-16 10:29:13 字數 4616 閱讀 5024

(一) 格式化處理

[c-sharp]view plain

copy

print?

var mydateformatter:dateformatter=new

dateformatter();  

mydateformatter.formatstring="yyyy-mm-dd jj:nn:ss"

;  mydateformatter.format(new

date());  

(二)字串轉換

自定義字串轉日期的函式,支援常用的豐富的日期格式

* 字串轉成日期,支援格式:

* yyyymmdd

* yyyymmddhh

* yyyymmddhhmm

* yyyymmddhhmmss

* yyyy-mm-dd("-"作判斷關鍵字)

* yyyy-mm-dd hh("-"作判斷關鍵字)

* yyyy-mm-dd hh("-"作判斷關鍵字)

* yyyy-mm-dd hh:mm ("-"作判斷關鍵字)

* yyyy-mm-dd hh:mm:ss("-"作判斷關鍵字)

* yyyy年mm月dd日("年"作判斷關鍵字)

* yyyy年mm月dd日hh時("年"作判斷關鍵字)

* yyyy年mm月dd日hh時mm分("年"作判斷關鍵字)

* yyyy年mm月dd日hh時mm分ss秒("年"作判斷關鍵字)

* mm/dd/yyyy(例如,"02/01/2005")

* mm/dd/yyyy hh:mm:ss

* mm/yyyy dd(例如,"02/2005 23")

* day month date hours:minutes:seconds gmt year(例如,"tue feb 1 00:00:00 gmt-0800 2005",這與 tostring() 一致)

* day month date year hours:minutes:seconds am/pm(例如,"tue feb 1 2005 12:00:00 am",這與 tolocalestring() 一致)

* day month date year hours:minutes:seconds(例如,"tue feb 1 2005 23:59:59")

* day month date year(例如,"tue feb 1 2005",這與 todatestring() 一致)

public static function parsedate(value:string):date

if (!value)

return new date();

var year:number;

var month:number;

var date:number;

var h:number = 0;

var m:number = 0;

var s:number = 0;

var ms:number = 0;

* yyyymmdd

* yyyymmddhh

* yyyymmddhhmm

* yyyymmddhhmmss

if(!isnan(number(value)) && value.length >= 8)

year = number(value.substr(0, 4));

month = number(value.substr(4, 2)) -1;

date = number(value.substr(6, 2));

if(value.length>=10)

h = number(value.substr(8, 2));

if(value.length>=12)

m = number(value.substr(10, 2));

if(value.length>=14)

s = number(value.substr(12, 2));

return new date(year, month, date,h,m,s,ms);

* yyyy-mm-dd("-"作判斷關鍵字)

* yyyy-mm-dd hh("-"作判斷關鍵字)

* yyyy-mm-dd hh("-"作判斷關鍵字)

* yyyy-mm-dd hh:mm ("-"作判斷關鍵字)

* yyyy-mm-dd hh:mm:ss("-"作判斷關鍵字)

* yyyy年mm月dd日("年"作判斷關鍵字)

* yyyy年mm月dd日hh時("年"作判斷關鍵字)

* yyyy年mm月dd日hh時mm分("年"作判斷關鍵字)

* yyyy年mm月dd日hh時mm分ss秒("年"作判斷關鍵字)

if(value.indexof("-")>0 || value.indexof("年")>0)

var match:array = value.match( //d+/g );

year = number(match[0]);

month = number(match[1]) -1;

date = number(match[2]);

if(match.length>3)

h = number(match[3]);

if(match.length>4)

m = number(match[4]);

if(match.length>5)

s = number(match[5]);

return new date(year, month, date,h,m,s,ms);

* mm/dd/yyyy(例如,"02/01/2005")

* mm/dd/yyyy hh:mm:ss

* mm/yyyy dd(例如,"02/2005 23")

* day month date hours:minutes:seconds gmt year(例如,"tue feb 1 00:00:00 gmt-0800 2005",這與 tostring() 一致)

* day month date year hours:minutes:seconds am/pm(例如,"tue feb 1 2005 12:00:00 am",這與 tolocalestring() 一致)

* day month date year hours:minutes:seconds(例如,"tue feb 1 2005 23:59:59")

* day month date year(例如,"tue feb 1 2005",這與 todatestring() 一致)

return new date(value);

測試通過的例子:

testdate("20111213");

testdate("2011121323");

testdate("201112132359");

testdate("20111213235959");

testdate("2011-12-13");

testdate("2011-12-13 23");

testdate("2011-12-13 23:59");

testdate("2011-12-13 23:59:59");

testdate("2023年12月13日");

testdate("2023年12月13日23時");

testdate("2023年12月13日23時59分");

testdate("2023年12月13日23時59分59秒");

testdate("12/13/2011");

testdate("12/13/2011 23:59:59");

testdate("12/2011 23");

testdate("fri dec 23 00:00:00 gmt+0800 2011");

testdate("fri dec 23 23:00:00 gmt+0800 2011");

testdate("fri dec 23 23:59:00 gmt+0800 2011");

testdate("fri dec 23 23:59:59 gmt+0800 2011");

testdate("fri dec 23 2011 12:00:00 am");

testdate("fri dec 23 2011 11:00:00 pm");

testdate("fri dec 23 2011 11:59:00 pm");

testdate("fri dec 23 2011 11:59:59 pm");

testdate("fri dec 23 2011 00:00:00");

testdate("fri dec 23 2011 23:00:00");

testdate("fri dec 23 2011 23:59:00");

testdate("fri dec 23 2011 23:59:59");

testdate("fri dec 23 2011");

Java日期時間格式化處理

date date new date string str string.format tc date 常用如下 te 乙個月中的某一天,如2 tb 指定言環境的月份簡稱,如feb 英文 二月 中文 tb 指定語言環境的月份全稱,如february 英文 二月 中文 ta 指定語言環境的星期全稱,如...

日期格式化處理函式集

ftimeformat item.updatetime,y m d author s字串格式替換 function fstring txt return txt 數字格式化 data 數字,f 格式串 格式串示例 000.00 表示整數部分至少三位小數部分兩位,前乙個0則不補0 必須指定小數點 fu...

python中字串的一些格式化處理

字串格式化處理一般有以下幾種 1.用 s或 d等進行格式化,如下 name input name age input age edu input edu salary input salary s name s age s educ s salary s name,age,edu,salary pr...