gpt4 book ai didi

javascript - 将 UK EndDate 与 UTC 当前时间进行比较?

转载 作者:行者123 更新时间:2023-12-01 16:17:30 24 4
gpt4 key购买 nike

如何将 UK endDate 转换为 UTC,以便与 Node 上的 UTC 当前时间 (nowTime) 进行比较?还是换一种方式更好?将当前 UTC 时间转换为伦敦时区,然后与英国 endDate

进行比较

例如:

const nowTime = new Date('Thu May 10 2020 15:55'); //mock now time in UTC

const date = { //this is london date/time
EndDate: "2020-05-10",
EndTime: "15:57",
}

const combineDateTime = date.EndDate + " " + date.EndTime;
const endDate = new Date(combineDateTime).getTime();

if (nowTime >= endDate) {
console.log('expired');
} else {
console.log('not expired')
}

最佳答案

由于您的本地时间是英国时间,这应该有效,否则您将不得不计算 dls

 var now = new Date();
var nowTime = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
console.log('UTC (in ms): ' + nowTime.getTime())
nowTimeUTC=nowTime.getTime()

const date = { //this is london date/time via API response
EndDate: "2020-06-12",
EndTime: "00:12",
}

servertime=new Date(date.EndDate.concat(" "+date.EndTime))
servertimeinmill=servertime.getTime()
utcoffset=(servertime.getTimezoneOffset()/60)*60*60*1000
servetimeinUTC=servertimeinmill+utcoffset
console.log(servetimeinUTC)
console.log(nowTime>servetimeinUTC)

关于javascript - 将 UK EndDate 与 UTC 当前时间进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62329938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com