完全兼容的Javascript时间格式化功能(更实际)

完全兼容的Javascript时间格式化功能(更实际)
javascript时间格式化功能的综合兼容性,实用总结!
复制代码代码如下所示:
js日期格式

*
*时间格式
* strdatetime格式化字符串:需要时间
*整数类型:格式类型
* /
功能formatdatetime(strdatetime,整数类型){
var年,月,日,小时,分钟,秒;
无功newdate,arrdate =新的数组(),arrtime =新的数组();
{试
如果(strdatetime!=定义的strdatetime!= null strdatetime!){
获取日期和时间数组
如果(strdatetime.indexof()!= 1){
VAR项= strdatetime.split();
arrdate =项目{ 0 }。toString()。分裂();
arrtime =项目{ 1 }。toString()。分裂(:);
} else if(strdatetime.indexof( / )!= 1){
VAR项= strdatetime.split();
arrdate =项目{ 0 }。toString()。分裂(/);
arrtime =项目{ 1 }。toString()。分裂(:);
}
数据处理
如果(arrdate!=定义的arrtime!=未定义)
arrdate.length = = 3 = = 3){ arrtime.length
newdate =新的日期(
ParseInt(arrdate { 0 }),
ParseInt(arrdate { 1 }),
ParseInt(arrdate { 2 }),
ParseInt(arrtime { 0 }),
ParseInt(arrtime { 1 }),
ParseInt(arrtime { 2 })
);
开关(数字(整数类型)){
案例1:格式:yyyy-mm-dd
年newdate.getfullyear();
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
newdate =年+-+月+-+天;
打破;
案例2::
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
小时= newdate.gethours();
如果(数字(小时)< 10)小时= 0+小时;
分钟= newdate.getminutes();
如果(数字(分钟)< 10)分钟= 0+分钟;
newdate =月+-+日+
+小时+ :+分钟;
打破;
案例3:格式:
小时= newdate.gethours();
如果(数字(小时)< 10)小时= 0+小时;
分钟= newdate.getminutes();
如果(数字(分钟)< 10)分钟= 0+分钟;
秒= newdate.getseconds();
如果(数(秒)<10)秒= 0 +秒;
newdate =时间+:+时间+:+秒;
打破;
案例4:格式为:mm
小时= newdate.gethours();
如果(数字(小时)< 10)小时= 0+小时;
分钟= newdate.getminutes();
如果(数字(分钟)< 10)分钟= 0+分钟;
newdate =时间+:+分钟;
打破;
案例5: / / yyyy-mm-dd格式:HH:毫米
年newdate.getfullyear();
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
小时= newdate.gethours();
如果(数字(小时)< 10)小时= 0+小时;
分钟= newdate.getminutes();
如果(数字(分钟)< 10)分钟= 0+分钟;
newdate =年+-+月+-+日+
+小时+ :+分钟;
打破;
案例6:格式: / / / DD yyyy mm
年newdate.getfullyear();
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
newdate =年+/+月+/+天;
打破;
第7个案例::
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
小时= newdate.gethours();
如果(数字(小时)< 10)小时= 0+小时;
分钟= newdate.getminutes();
如果(数字(分钟)< 10)分钟= 0+分钟;
newdate =月+/+日+
+小时+ :+分钟;
打破;
案例8: / / / / DD yyyy mm格式:HH:毫米
年newdate.getfullyear();
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
小时= newdate.gethours();
如果(数字(小时)< 10)小时= 0+小时;
分钟= newdate.getminutes();
如果(数字(分钟)< 10)分钟= 0+分钟;
newdate =年+/+月+/+日+
+小时+ :+分钟;
打破;
案例9:格式:YY
年newdate.getfullyear();
年years.tostring()Substr(2, 2);
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
newdate =年+-+月+-+天;
打破;
案例10:格式:YY /毫米/ DD
年newdate.getfullyear();
年years.tostring()Substr(2, 2);
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
newdate =年+/+月+/+天;
打破;
案例/ 11:格式:yyyy年MM月DD HH毫米
年newdate.getfullyear();
月= newdate.getmonth();
如果(数字(月)< 10)月= 0+月;
天newdate.getdate();
如果(数字(天)< 10)天= 0 +天;
小时= newdate.gethours();
如果(数字(小时)< 10)小时= 0+小时;
分钟= newdate.getminutes();
如果(数字(分钟)< 10)分钟= 0+分钟;
newdate =年+年+月+月+日+
++时间++分钟+ 子;
打破;
}
}
}
} catch(e){
newdate =新的日期();
返回newdate.getfullyear()+-+
(newdate.getmonth)+(1)+-+
newdate.getdate()++
newdate.gethours()+:+
newdate.getminutes()+:+
NewDate.getSeconds();
}
返回newdate;
}

调用
document.writeln(formatdatetime(2014 / 04 / 16 22:34:45
免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部