第一种
// type为true只获取时间戳10位 否则把连带毫秒数都拿出来
timestamp(type) {
var timestamp = Date.parse(new Date());
if (type != undefined) {
var temp = timestamp.toString();
timestamp = temp.substr(0, 10);
timestamp = parseInt(timestamp);
}
return timestamp;
}
console.log(timestamp(true));
第二种
var TIMESTAMP = parseInt(new Date().getTime() / 1000);
第三种
var timestamp = (new Date('2019-10-13')).valueOf()
Date对象方法
方法 | 说明 |
---|---|
getDate() | 从 Date 对象返回一个月中的某一天 (1 ~ 31) |
getDay() | 从 Date 对象返回一周中的某一天 (0 ~ 6) |
getMonth() | 从 Date 对象返回月份 (0 ~ 11) |
getFullYear() | 从 Date 对象以四位数字返回年份 |
getHours() | 返回 Date 对象的小时 (0 ~ 23) |
getMinutes() | 返回 Date 对象的分钟 (0 ~ 59) |
getSeconds() | 返回 Date 对象的秒数 (0 ~ 59) |
getMilliseconds() | 返回 Date 对象的毫秒(0 ~ 999) |
getTime() | 返回 1970 年 1 月 1 日至今的毫秒数 |