本文介绍了JavaScript中获取当前时间戳的三种常用方法:Date.parse()、valueOf()和getTime()。重点分析了前两种方法在毫秒精度上的差异,指出Date.parse()会将毫秒置零,可能导致时间差计算误差。通过实际案例说明在需要精确时间差的场景中,应优先使用valueOf()或getTime()以保留毫秒信息。
JavaScript 获取当前时间戳的三种方法及注意事项
对 JavaScript 中 new Date() 构造函数进行基础测试。
var t = new Date().toString(); //t = "Thu Oct 31 2019 11:36:57 GMT+0800 (中国标准时间)"
var t1 = new Date();
var data_arry =[];
var that = new Date();
data_arry.push(that.constructor()); // Thu Oct 31 2019 11:50:26 GMT+0800 (中国标准时间)"
data_arry.push(that.getDate()); //31
data_arry.push(that.getDay()); // 4
data_arry.push(that.getFullYear()); //2019
data_arry.push(that.getHours()); // 11
data_arry.push(that.getMilliseconds()); // 401
data_arry.push(that.getMinutes()); // 50
data_arry.push(that.getMonth()); //9
data_arry.push(that.getSeconds()); //26
data_arry.push(that.getTime()); //1572493826401
data_arry.push(that.getTimezoneOffset()); //-480
data_arry.push(that.getUTCDate()); //31
data_arry.push(that.getUTCDay());
data_arry.push(that.getUTCFullYear());
data_arry.push(that.getUTCHours());
data_arry.push(that.getUTCMilliseconds());
data_arry.push(that.getUTCMinutes());
data_arry.push(that.getUTCMonth());
data_arry.push(that.getUTCSeconds());
data_arry.push(that.getYear());
data_arry.push(that.setDate());
data_arry.push(that.setFullYear());
data_arry.push(that.setHours());
data_arry.push(that.setMilliseconds());
data_arry.push(that.setMinutes());
data_arry.push(that.setMonth());
data_arry.push(that.setSeconds());
data_arry.push(that.setTime());
data_arry.push(that.setUTCDate());
data_arry.push(that.setUTCFullYear());
data_arry.push(that.setUTCHours());
data_arry.push(that.setUTCMilliseconds());
data_arry.push(that.setUTCMinutes());
data_arry.push(that.setUTCMonth());
data_arry.push(that.setUTCSeconds());
data_arry.push(that.setYear());
data_arry.push(that.toDateString());
data_arry.push(that.toGMTString());
//data_arry.push(that.toISOString()); 报错
data_arry.push(that.toJSON());
data_arry.push(that.toLocaleDateString());
data_arry.push(that.toLocaleString());
data_arry.push(that.toLocaleTimeString());
data_arry.push(that.toString());
data_arry.push(that.toTimeString());
data_arry.push(that.toUTCString());
data_arry.push(that.valueOf());
相关函数说明。
function get_nowtime() {
var date = new Date();
return (date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
}
console.log(get_nowtime());
扩展阅读内容如下。
JavaScript 获取当前时间戳的常见方式。
具体实现 JavaScript 获取当前时间戳的方法包括以下几种。
第一种实现方式:
第一种方法:Date.parse()
var timestamp =Date.parse(new Date());
运行结果示例:1280977330000
第二种实现方式:
第二种方法:valueOf()
var timestamp =(new Date()).valueOf();
运行结果示例:1280977330748
第三种实现方式:
第三种方法:getTime()
var timestamp=new Date().getTime();
运行结果示例:1280977330748
第一种方法获取的时间戳会将毫秒部分强制显示为 000。
第二种和第三种方法则能够获取包含当前毫秒数的精确时间戳。
时间计算精度问题分析
在开发过程中,我和同事尝试使用 JavaScript 计算数据分析所需的剩余时间,但发现计算结果经常异常地变为 0。经过排查,问题根源在于使用了 Date.parse(new Date()) 方法,该方法会将毫秒部分截断为 000,从而导致时间差计算出现严重偏差。
建议在计算时间差时,采用第二种或第三种方法来确保精度。
如果在 JavaScript 中单独调用 new Date(),例如执行 document.write(new Date());
页面显示的时间格式通常为:Mar 31 10:10:43 UTC+0800 2012 这种标准格式。
但是用new Date() 参与计算会自动转换为从1970.1.1开始的毫秒数
以上内容涵盖了本次介绍的所有知识点及扩展信息,感谢读者的学习以及对本站的支持。