在 JavaScript 中获取今天从 - 到时间戳

所以我们可以用 获取当前时间戳new Date().getTime(),但是我如何计算今天的从到时间戳(从午夜到午夜)?



慕森王
浏览 168回答 3
3回答

慕莱坞森

如果你想从今天午夜到明天午夜,不使用任何 3rd 方库。你可以这样做->const from = new Date();const to = new Date(from);function clearTime(t) {  t.setMinutes(0);  t.setSeconds(0);  t.setMilliseconds(0);}from.setHours(0); clearTime(from);to.setHours(24); clearTime(to);console.log(from);console.log(to);

SMILET

如果你想从时间戳创建日期,你可以使用 new Date(timestamp)let timestamp = 1584532480757;let date = new Date(timestamp );document.write(date);

繁花不似锦

您可以使用以下内容。如果您需要对下面的代码片段进行解释,请告诉我。var now = new Date();var [year, month, day] = [now.getFullYear(), now.getMonth(), now.getDate()];var todayFrom = new Date(year, month, day);var todayTo = new Date(year, month, day+1);todayTo = new Date(todayTo.getTime()-1);console.log(todayFrom);console.log(todayTo);console.log(todayFrom.getTime());console.log(todayTo.getTime());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript