获取JavaScript中未来日期和时间的Unix时间戳

huangapple go评论66阅读模式
英文:

Getting a Unix Timestamp for a future date and time in JavaScript

问题

如何在JavaScript中获取未来的Unix时间戳?即,传入"2023-03-08 13:05:00"并获得相应的Unix时间戳。

我找到了一个在PHP中的解决方案,我认为它可以满足我寻找的目的:
使用特定日期和时间获取未来的Unix时间戳?

在JavaScript中是否有类似的函数?

英文:

How can I get a future unix timestamp in Javacript? ie, pass "2023-03-08 13:05:00" and get a unix timestamp corresponding to that time.

All the examples I see use the getTime() function which is the current time like this:
Javascript: future time as UNIX timestamp

I found a solution in Php which I think serves the purpose I am looking for:
Getting a future unix timestamp using a specific date and time?

Is there a similar function in Javascript?

答案1

得分: 1

// 创建一个表示将来时间的日期对象
var futureTime = new Date("2023-03-08 13:05:00");

// 获取将来时间的Unix时间戳(毫秒)
var futureTimestamp = futureTime.getTime();

// 将毫秒转换为秒
var futureUnixTimestamp = Math.floor(futureTimestamp / 1000);

console.log(futureUnixTimestamp); // 输出:1678385100

英文:
// Create a Date object for the future time

var futureTime = new Date("2023-03-08 13:05:00");

// Get the Unix timestamp (in milliseconds) for the future time

var futureTimestamp = futureTime.getTime();

// Convert milliseconds to seconds

var futureUnixTimestamp = Math.floor(futureTimestamp / 1000);

console.log(futureUnixTimestamp); // Output: 1678385100

huangapple
  • 本文由 发表于 2023年3月7日 08:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定