截取时间日期字符串中的日期部分,而后构造 Date 对象,最后输出的结果不是想要的结果,例如,函数
// 下面的这个时间是错误的, // 缘由待探讨 > d = new Date() > new Date(d.toISOString().slice(0, 10)).getTime()
究竟是截取去掉的时间不正确仍是 Date 构造函数输入的值不正确,因而引出一个问题,“实例一个 Date 对象,参数形式不一样,会有相同的结果吗?”以下示例:code
// 在东八区,输出 false new Date(2019, 5, 5) === new Date('2019-06-05')
因此,日期控件输出,以及日期时间戳的计算,统一用标准时间格式。对象
// 今天的时间戳 function today() { return moment().startOf('day').valueOf(); }
test('today', () => { const todayTimestamp = today(); const nowDate = new Date(); const UTCFullYear = nowDate.getUTCFullYear(); const UTCMonth = nowDate.getUTCMonth(); const UTCDate = nowDate.getUTCDate(); const UTCTimestamp = new Date(UTCFullYear, UTCMonth, UTCDate).getTime(); expect(todayTimestamp).toEqual(UTCTimestamp); });