js
最简单获取时间戳的方式是 var time = +new Date;console.log(time)然后格式化Date.prototype.datetime = function() {return myDate.getFullYear() + '-' +('0' + (myDate.getMonth()+1)).slice(-2)+ '-' + myDate.getDate() + ' '+myDate.getHours()+ ':'+('0' + (myDate.getMinutes())).slice(-2)+ ':'+myDate.getSeconds(); };16可以替换成 timevar myDate = new Date(16); myDate.datetime();将输出 2018-04-14 00:06:01
还可以使用moment.js(下载地址 http://momentjs.com/)直接调用format格式化即可。var date = new Date(); var formattedDate = moment(date).format('YYYYMMDD');
把一个日期对象转换为可读字符串var ts = new Date();console.log(ts.toDateString())# Fri Apr 13 2018
返回ISO标准的日期var ts = new Date();console.log(ts.toISOString());# 2018-04-13T11:42:30.244Z
toJSON 返回json形式的日期var ts = new Date();console.log(ts.toJSON());# 2018-04-13T11:42:30.244Z
toLocaleDateString 返回本地区域日期var ts = new Date();console.log(ts.toLocaleDateString());# 2018/4/13
toLocaleTimeString 返回本地区域时间var ts = new Date();console.log(ts.toLocaleTimeString());# 下午7:42:30
toLocaleString 把本地区域日期转换为字符串var ts = new Date();console.log(ts.toLocaleString());# 2018/4/13 下午7:42:30
toString 把日期转换为字符串var ts = new Date();console.log(ts.toString());# Fri Apr 13 2018 19:42:30 GMT+0800 (中国标准时间)
toTimeString 把时间部分转换为字符串var ts = new Date();console.log(ts.toTimeString());# 19:42:30 GMT+0800 (中国标准时间)
toUTCString() 根据通用时间将 Date 对象转换为字符串var ts = new Date();console.log(ts.toUTCString());# Fri, 13 Apr 2018 11:42:30 GMT