獲取時間並格式化時間格式

dateFormat: function(time) {

var date = new Date(time);

var year = date.getFullYear();

/* 在日期格式中,月份是從0開始的,因此要加0

* 使用三元表達式在小於10的前面加0,以達到格式統一 如 09:11:05

* */

var month =

date.getMonth() + 1 < 10

? "0" + (date.getMonth() + 1)

: date.getMonth() + 1;

var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

var hours =

date.getHours() < 10 ? "0" + date.getHours() : date.getHours();

var minutes =

date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();

var seconds =

date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

// 拼接

return (

year +

"-" +

month +

"-" +

day +

" " +

hours +

":" +

minutes +

":" +

seconds

);

},

調用  date=dateFormat("YYYY-mm-dd HH:MM:SS", date)

當前時間 this.dateFormat(new Date())

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章