如何在javascript中获取第二天上午8点的日期时间对象?
我尝试了新的Date()
答案 0 :(得分:0)
效率不高,但这是一个简单的例子。
var today = new Date();
var diff = new Date(
today.getFullYear(),
today.getMonth(),
today.getDate() + 1,
8
).getTime() - today.getTime();
console.log((diff / 1000) | 0);
答案 1 :(得分:-1)
使用Node进行日期和时间操作很困难。 IF 您的计算机上有PHP,我建议
var child_process = require('child_process');
var command = 'php NumSecondsTo8am.php';
child_process.exec(command, function(error, stdout, stderr) {
console.log(stdout, 'seconds');
});
然后,在同一目录中的一个单独文件中(我称为 numSecondsTo8am.php )
<?php
# numSecondsTo8am.php
date_default_timezone_set('America/Chicago');
# get the current time
$currentTime = new DateTime();
# get the current time in Los Angeles, and add a day to it
$tomorrow = new DateTime();
$tomorrow->setTimezone(new DateTimeZone('America/Los_Angeles'));
$dateInterval = new DateInterval('P1D');
$tomorrow->add($dateInterval);
# since we want to know about 8am, change the hour to 8am
$tomorrow->setTime(8, 0, 0);
# get the timestamp the future date will produce and subtract it from
# our current timestamp
$numSeconds = intval($tomorrow->format('U')) -
intval($currentTime->format('U'));
echo "$numSeconds";
请记住将时区字符串更改为您当地的时区!否则,答案肯定不正确。
如果您正在寻找纯粹的Node解决方案,我还没找到。 即使使用Moment也不会给你正确答案