JS---ES6格式化日期輸出

引子

        今天在學習DOM的時候,做一個修改元素的案例,想要做個簡單的獲取時間的網頁,發現在格式化輸出時es6版本的JS已經刪除了format()函數,沒辦法,只能自己手寫一個。

實現代碼

// 補零函數
function JoinZero(num) {
	return num < 10 ? '0' + num : num;
}
// 獲取時間
function getTime(date) {
	var time = (date == null ? new Date() : new Date(date));
	let y = time.getFullYear();
	let m = JoinZero(time.getMonth() + 1);
	let d = JoinZero(time.getDate());
	let h = JoinZero(time.getHours());
	let mi = JoinZero(time.getMinutes());
	let s = JoinZero(time.getSeconds());
	return `${y}-${m}-${d}-${h}-${mi}-${s}`;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章