Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当前时间和3500毫秒之前的Javascript UTC时间戳_Javascript_Time - Fatal编程技术网

当前时间和3500毫秒之前的Javascript UTC时间戳

当前时间和3500毫秒之前的Javascript UTC时间戳,javascript,time,Javascript,Time,拜托,有人告诉我怎么做。我已经尝试了我找到的每一种方法。什么都没用。我不想进口任何东西。我只需要当前的时间戳和3500毫秒。这是一个API调用 以下是我目前的情况: var current_date = new Date().toLocaleString("en-US", {timeZone: "Europe/London"}); var start_date = new Date(); start_date.setMinutes(start_date.getMinutes() - 3);

拜托,有人告诉我怎么做。我已经尝试了我找到的每一种方法。什么都没用。我不想进口任何东西。我只需要当前的时间戳和3500毫秒。这是一个API调用

以下是我目前的情况:

var current_date = new Date().toLocaleString("en-US", {timeZone: "Europe/London"});


var start_date = new Date();
start_date.setMinutes(start_date.getMinutes() - 3); // timestamp
start_date = new Date(start_date).getTime(); // Date object


我不知道您的API调用是采用unix时间戳还是ISO-8601字符串(或其他日期格式)。 使用本机JavaScript日期对象,您可以轻松获得两者中的UTC时间

将始终以毫秒为单位给出从unix纪元开始的时间,这与我们调用它的时区无关。如果需要以秒为单位的unix时间戳,只需除以1000即可

同样地,我们将以格式提供当前UTC时间,因此这也将是明确的

向日期添加偏移量(以毫秒为单位)很简单,只需将偏移量添加/减去unix历元偏移量(return of date.getTime()),然后传递给日期构造函数即可获得偏移量日期

如果您希望发送我建议使用的US/GB等格式字符串,您可以使用“UTC”作为时区(如果您遵循该方法)

如果要创建自定义UTC时间字符串,可以始终使用、、等并组合创建日期字符串

const locale=“en-us”;
const currentTimeUnix=new Date().getTime();
const currentDate=新日期(currentTimeUnix);
log(`CurrentUTC时间(unix,ms):${currentTimeUnix}`);
console.log(`当前UTC时间(unix,秒):${Math.round(currentTimeUnix/1000)}`);
log(`CurrentUTC时间(ISO-8601):${currentDate.toISOString()}`);
log(`currentUTC时间(区域设置格式):`,currentDate.toLocaleString(区域设置,{timeZone:'UTC'}));
常数偏移毫秒=3500;
//从当前时间中减去偏移量
const offsetTimeUnix=currentTimeUnix-offsetMillimes;
const offsetDate=新日期(offsetTimeUnix);
log(`\n减去偏移量(${offsetmillizes}ms)后的结果:`);
log(`Offset UTC time(unix,ms):${offsetTimeUnix}`);
log(`Offset UTC time(unix,sec):${Math.round(offsetTimeUnix/1000)}`);
log(`Offset UTC time(ISO-8601):${offsetDate.toISOString()}`);
log(`Offset UTC time(locale格式):`,offsetDate.toLocaleString(locale,{timeZone:'UTC'}));
//注意:在JavaScript中,月份是以零为基础的
const customTimestamp=`${offsetDate.getUTCFullYear()}-${offsetDate.getUTCMonth()+1}-${offsetDate.getUTCDate()}${offsetDate.getUTCHours()}:${offsetDate.getUTCMinutes()}:${offsetDate.getUTCSeconds()};

log(`Offset UTC time(自定义格式):`,customTimestamp)这相当容易做到

getTime()方法返回自Unix纪元以来的毫秒数*

*JavaScript使用毫秒作为度量单位,而Unix时间以秒为单位

getTime()始终使用UTC表示时间。例如,一个时区中的客户端浏览器getTime()将与任何其他时区中的客户端浏览器相同

由于JS使用毫秒时间戳,所以只需获取当前时间的时间戳(始终为UTC),然后从中减去毫秒数即可

让currentTimestamp=new Date().getTime();
设threePointFiveSecondsAgo=currentTimestamp-3500;
//将时间戳转换回日期。
让pastDate=新日期(三点五秒前);

3500ms为3.5秒。不用了,谢谢。不幸的是,这不是我想要的答案。忽略毫秒数。