<script>
function format(newDate) {
var day = newDate.getDay()
var y = newDate.getFullYear()
var m = (newDate.getMonth() + 1) < 10 ? "0" + (newDate.getMonth() + 1) : (newDate.getMonth() + 1)
var d = newDate.getDate() < 10 ? "0" + newDate.getDate() : newDate.getDate()
var h = newDate.getHours() < 10 ? "0" + newDate.getHours() : newDate.getHours()
var min = newDate.getMinutes() < 10 ? "0" + newDate.getMinutes() : newDate.getMinutes()
var s = newDate.getSeconds() < 10 ? "0" + newDate.getSeconds() : newDate.getSeconds()
var dict = {
1: "一",
2: "二",
3: "三",
4: "四",
5: "五",
6: "六",
0: "天",
}
//var week=["日","一","二","三","四","五","六"]
return y + "-" + m + "-" + d + " " + h + ":" + min + ":" + s + " 星期" + dict[day]
}
var newDate = new Date()
console.log(format(newDate))
</script>
注意点:
一、date.getDay()的结果为数字0-6,0对应星期天、1-6对应星期一到星期六
将数字转化为中文,有两种方法:
(1)字典 var dict = {
1: "一",
2: "二",
3: "三",
4: "四",
5: "五",
6: "六",
0: "天",
}
使用:dict[date.getDay()]
(2)数组 var week=["日","一","二","三","四","五","六"]
使用:week[date.getDay()]
二、date.getMonth()得到的月份+1才为真实月份
打印结果