js 时间格式化

家电维修 2022-11-07 13:31www.17kangjie.cn家电维修培训

js怎么时间格式化呢?不知道的小伙伴来看看长沙家政网小编今天的分享吧!

js怎么格式化时间有三种格式:

格式一:2018-1-29-10:34:49

var curr_time = ne Date();

Myformatter(curr_time);

function myformatter(date){         

var strDate = date.getFullYear()+"-"; 

strDate += date.getMonth()+1+"-";       

strDate += date.getDate()+"-";      

strDate += date.getHours()+":";     

strDate += date.getMinutes()+":";       

strDate += date.getSeconds();     

alert("strDate:"+strDate);      

return strDate ;  

格式二: 2018-1-29

function myformatter(date){  

var strDate = date.getFullYear()+"-";

strDate += date.getMonth()+1+"-";

strDate += date.getDate();

alert("strDate:"+strDate);

return strDate ;

}

格式三:2018-02-05   (月份和日期小于10的时候,在前面自动加零)

function myformatter(date){  

var strDate = date.getFullYear()+"-";

if(date.getMonth()<10){

var s = date.getMonth()+1+"-";

strDate += "0"+s;

}else{

strDate += date.getMonth()+1+"-";

}

if(date.getDate()<10){

strDate += "0"+date.getDate();

}else{

strDate += date.getDate();

}

return strDate ;


以上就是长沙家政网小编今天的分享了,希望可以帮助到大家。


Copyright © 2016-2025 www.17kangjie.cn 长沙家政网【一起康洁家政】 版权所有 Power by