将给定的 unix 时间字符串 (EST) 转换为本地服务器时间 (CST) 并评估 unix 时间和当前时间之间的差异
Convert a given unix time string (EST) to local server time (CST) and evaluate difference between unix time and current time
我一直在努力使用 javascript 比较两个 date/times。我正在尝试评估提供的 unix 时间字符串(大概是 EST)与 javascript 格式的当前时间(服务器位于 运行 的任何时区)之间的差异,并获得 hours/minutes/milliseconds.
我相信我可以尝试/已经尝试过三种方法:
- 将给定时区的当前服务器的javascript时间转换为unix时间,然后从中减去提供的unix时间
- 根据服务器的时区将提供的unix时间转换为javascript格式,然后从服务器的当前时间中减去它
- 将两个时间都转换为 UTC 或 GMT 并对其进行计算
我搜索了 Whosebug 并为这些方法尝试了许多不同的方法,但未能获得准确的值。
例如,如果提供的unix时间是1599206400000
,而当前服务器的javascript时间是Fri Sep 04 2020 16:47:26 GMT-0500 (Central Daylight Time)
,我如何得到两者之间的毫秒差异?
很抱歉没有代码示例。原因是我尝试了很多不同的代码修改都没有成功,我面前的东西不再反映所有这些努力。
如果可以的话请帮忙,谢谢!我连续两天都在为这个问题苦苦挣扎!
我。您可以使用 momentjs 来解决这个问题:
timestampInMilliseconds = 1599206400000;
currentTimestampInMilliseconds = parseInt(moment.utc().format('x'));
currentTimestampInMilliseconds - timestampInMilliseconds;
// Example return value 94092301
要获取 UTC
中的当前时间,请使用:moment.utc()
要从以毫秒为单位的 momentJS 日期获取时间戳,请使用:format('x')
您还需要将其转换为 int
(借助 parseInt
函数)直接导致 format
函数的输出类型为 string
二.普通的JS解决方案更简单:
timestampInMilliseconds = 1599206400000;
currentTimestampInMilliseconds = (new Date).getTime();
currentTimestampInMilliseconds - timestampInMilliseconds;
// Example return value 94092301
在这种情况下你可以自由使用getTime()
,因为官方文档说:getTime() always uses UTC for time representation
// timestamp
var d = new Date()
d.setHours(4) // 4am
d.setMinutes(00) //00 minutes
var unixTimeStamp = Math.floor(d.getTime() / 1000); // converts to unix time
console.log(unixTimeStamp)
var apicurrentTime
var timeStamp
var differenceintime = apicurrentTime - timeStamp
- 服务器:推荐使用UTC(ISO-8601格式)。这是标准的序列化格式。
- 客户端: 客户端可以在本地处理时间并发送 UTC equiv。使用
toISOString
到服务器。
如果您正在寻找纯 JS 代码,getTime()
- 这个 returns Unix 时间是 timezone-free。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
举个例子:
const timeA = 1599206400000; // already Unix time
const timeB = new Date(2020, 08, 04, 16, 47, 26, 531).getTime(); // getTime()
console.log(timeA, timeB, difference(timeB - timeA));
输出:
1599206400000 1599218246531 {hour: 3, min: 17, seconds: 26, milliseconds: 531}
difference
函数的定义:
const difference = (millis) => {
const parts = [(millis / 3600000) % 60, (millis / 60000) % 60, (millis / 1000) % 60, millis % 1000];
const [hour, min, seconds, milliseconds] = parts.map((v) => parseInt(v));
return { hour, min, seconds, milliseconds };
};
我一直在努力使用 javascript 比较两个 date/times。我正在尝试评估提供的 unix 时间字符串(大概是 EST)与 javascript 格式的当前时间(服务器位于 运行 的任何时区)之间的差异,并获得 hours/minutes/milliseconds.
我相信我可以尝试/已经尝试过三种方法:
- 将给定时区的当前服务器的javascript时间转换为unix时间,然后从中减去提供的unix时间
- 根据服务器的时区将提供的unix时间转换为javascript格式,然后从服务器的当前时间中减去它
- 将两个时间都转换为 UTC 或 GMT 并对其进行计算
我搜索了 Whosebug 并为这些方法尝试了许多不同的方法,但未能获得准确的值。
例如,如果提供的unix时间是1599206400000
,而当前服务器的javascript时间是Fri Sep 04 2020 16:47:26 GMT-0500 (Central Daylight Time)
,我如何得到两者之间的毫秒差异?
很抱歉没有代码示例。原因是我尝试了很多不同的代码修改都没有成功,我面前的东西不再反映所有这些努力。
如果可以的话请帮忙,谢谢!我连续两天都在为这个问题苦苦挣扎!
我。您可以使用 momentjs 来解决这个问题:
timestampInMilliseconds = 1599206400000;
currentTimestampInMilliseconds = parseInt(moment.utc().format('x'));
currentTimestampInMilliseconds - timestampInMilliseconds;
// Example return value 94092301
要获取
UTC
中的当前时间,请使用:moment.utc()
要从以毫秒为单位的 momentJS 日期获取时间戳,请使用:
format('x')
您还需要将其转换为
int
(借助parseInt
函数)直接导致format
函数的输出类型为string
二.普通的JS解决方案更简单:
timestampInMilliseconds = 1599206400000;
currentTimestampInMilliseconds = (new Date).getTime();
currentTimestampInMilliseconds - timestampInMilliseconds;
// Example return value 94092301
在这种情况下你可以自由使用getTime()
,因为官方文档说:getTime() always uses UTC for time representation
// timestamp
var d = new Date()
d.setHours(4) // 4am
d.setMinutes(00) //00 minutes
var unixTimeStamp = Math.floor(d.getTime() / 1000); // converts to unix time
console.log(unixTimeStamp)
var apicurrentTime
var timeStamp
var differenceintime = apicurrentTime - timeStamp
- 服务器:推荐使用UTC(ISO-8601格式)。这是标准的序列化格式。
- 客户端: 客户端可以在本地处理时间并发送 UTC equiv。使用
toISOString
到服务器。
如果您正在寻找纯 JS 代码,getTime()
- 这个 returns Unix 时间是 timezone-free。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
举个例子:
const timeA = 1599206400000; // already Unix time
const timeB = new Date(2020, 08, 04, 16, 47, 26, 531).getTime(); // getTime()
console.log(timeA, timeB, difference(timeB - timeA));
输出:
1599206400000 1599218246531 {hour: 3, min: 17, seconds: 26, milliseconds: 531}
difference
函数的定义:
const difference = (millis) => {
const parts = [(millis / 3600000) % 60, (millis / 60000) % 60, (millis / 1000) % 60, millis % 1000];
const [hour, min, seconds, milliseconds] = parts.map((v) => parseInt(v));
return { hour, min, seconds, milliseconds };
};