javascript - 从两种不同的日期方法获取毫秒
问题描述
为什么这两种获取毫秒的方式有区别
gigasecond = inputDate => {
console.log(inputDate)
console.log(inputDate.getTime()) //1303689600000
console.log(
Date.UTC(inputDate.getFullYear(),
inputDate.getMonth(),
inputDate.getDate(),
inputDate.getHours(),
inputDate.getMinutes(),
inputDate.getSeconds())); //1303709400000
};
gigasecond(new Date(Date.UTC(2011, 3, 25)))
解决方案
该getHours()
方法根据本地时间返回指定日期的小时。改为使用getUTCHours()
。
gigasecond = inputDate => {
console.log(inputDate)
console.log(inputDate.getHours(), inputDate.getUTCHours())
console.log(inputDate.getTime()) //1303689600000
console.log(
Date.UTC(inputDate.getFullYear(),
inputDate.getUTCMonth(),
inputDate.getUTCDate(),
inputDate.getUTCHours(),
inputDate.getUTCMinutes(),
inputDate.getUTCSeconds()
)
); //1303709400000
};
gigasecond(new Date(Date.UTC(2011, 3, 25)))
推荐阅读
- delphi - 如何保存指向接口方法的指针?
- amazon-web-services - AWS CloudFormation 在创建额外服务时出错
- rest - 在 nifi 中获取源/上游连接的处理器名称
- javascript - 显示以角度 7 隐藏 div
- javascript - 如何在 Primefaces 中的 keyup 事件上添加延迟过滤功能?
- python - 使用 matplotlib 绘制熊猫数据框时出现 KeyError
- javascript - 从 Swagger 文件中捕获参数
- node.js - 基于 Node.js 资源的 ACL
- marklogic-9 - 使用 maven 和 spring boot 部署 Datahub
- datetime - 如何在 RobotFramework 中将日期格式化为法语值?