【发布时间】:2021-12-01 04:34:39
【问题描述】:
所以这就是下面的代码应该做的。我本质上是在制作在不同时区的午夜标记到期的对象。我认为我应该这样做的方式是首先获取用户位置并找到相应的时区。从那里获取该时区的当前日期并创建一个时间格式的字符串 (currentTimestamp),使其在一天结束时的晚上 11:59:59 到期。然后将此时间转换为 UTC(因为这就是服务器的运行方式),然后显然我最终会在时间过去后从我的数据库中删除该对象。例如,我试图让它在上海时区工作,但我得到了一个奇怪且非常错误的 UTC 时刻值。这是它的样子:
let timezone = tzlookup(latitude, longitude);
let currentDate = new Date()
let localizedDateAndTime = moment.tz(currentDate, timezone).format('YYYY-MM-DD hh:mm:ss')
let localizedDate = moment.tz(currentDate, timezone).format('YYYY-MM-DD')
console.log("Timezone: " + timezone)
console.log("Local Time: " + localizedDateAndTime)
console.log("UTC Time: " + moment.tz(currentDate, 'UTC').format())
let date = ("0" + currentDate.getDate()).slice(-2);
let month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
let year = currentDate.getFullYear();
let currentTimestamp = localizedDate + " " + 23 + ":" + 59 + ":" + 59
console.log("Localized timestamp: " + currentTimestamp)
var localizedMoment = moment.tz(currentTimestamp, 'YYYY-MM-DD hh:mm:ss', timezone);
var utcMoment = localizedMoment.utc()
console.log("UTC Moment (when event expires in UTC): " + utcMoment.format('YYYY-MM-DD hh:mm:ss'))
let timestamp = utcMoment.format('YYYY-MM-DD hh:mm:ss')
https://i.stack.imgur.com/deRbS.png
所以我想现在不应该是 03:59:59,而是应该是 15:59:59,对吧?不知道为什么它会转换为 03:59:59...
【问题讨论】:
-
“所以我想现在不应该是 03:59:59,而是应该是 15:59:59,对吧?不知道为什么它会转换为 03:59 :59..." 因为这就是你要求它做的事情,
hhformat token 在这里:.format('YYYY-MM-DD hh:mm:ss')。如果您想要 24 小时制,请改用HH。
标签: javascript node.js datetime momentjs moment-timezone