https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var adate = new Date();
var newdate = adate.toLocaleString();
console.log(newdate);
---------------------
输出结果:2018-10-16 15:42:53

//nodejs时间格式化
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //season
"S": this.getMilliseconds() //msec
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

var time1 = new Date().Format("yyyy-MM-dd");

var time2 = new Date().Format("yyyy-MM-dd hh: mm: ss: S");

console.log("time1: " + time1);
console.log("time2: " + time2);

------------------------------------
输出结果:
time1: 2018-10-16
time2: 2018-10-16 15: 42: 53: 276