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