英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论