當前位置:生活全書館 >

IT科技

> js當前時間格式化

js當前時間格式化

js怎麼格式化時間有三種格式:

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

var curr_time = new 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 ;  

js當前時間格式化

格式二: 2018-1-29

function myformatter(date){  

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

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

strDate += date.getDate();

alert("strDate:"+strDate);

return strDate ;

}

js當前時間格式化 第2張

格式三: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 ;


以上就是小編今天的分享了,希望可以幫助到大家。


標籤: 格式化 js 時間
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/eq1plq.html