如何在时间戳(GMT)中开始(00:00:00)和结束(23:59:59)今天?电脑使用当地时间。
当前回答
根据评分最高的答案,但要在一行中定义日期:
const startToday = new Date(new Date().setUTCHours(0,0,0,0));
const endToday = new Date(new Date().setUTCHours(23,59,59,999));
解释:
new Date().setUTCHours(0,0,0,0) // returns the epoch time number
new Date(/* epoch number */) // returns that epoch Date object
这就是为什么需要两个新的Date构造函数。
其他回答
这可能有点棘手,但你可以使用Intl.DateTimeFormat。
下面的代码片段可以帮助您将任何时区的任何日期转换为开始/结束时间。
const beginingOfDay = (options = {}) => { const { date = new Date(), timeZone } = options; const parts = Intl.DateTimeFormat("en-US", { timeZone, hourCycle: "h23", hour: "numeric", minute: "numeric", second: "numeric", }).formatToParts(date); const hour = parseInt(parts.find((i) => i.type === "hour").value); const minute = parseInt(parts.find((i) => i.type === "minute").value); const second = parseInt(parts.find((i) => i.type === "second").value); return new Date( 1000 * Math.floor( (date - hour * 3600000 - minute * 60000 - second * 1000) / 1000 ) ); }; const endOfDay = (...args) => new Date(beginingOfDay(...args).getTime() + 86399999); const beginingOfYear = () => {}; console.log(beginingOfDay({ timeZone: "GMT" })); console.log(endOfDay({ timeZone: "GMT" })); console.log(beginingOfDay({ timeZone: "Asia/Tokyo" })); console.log(endOfDay({ timeZone: "Asia/Tokyo" }));
// get current time for UTC timezone
const d = new Date();
const year = d.getUTCFullYear();
const month = d.getUTCMonth();
const day = d.getUTCDate();
// set time to begin day UTC
const startTime = Date.UTC(year, month, day, 0, 0, 0, 0);
//set time to end day UTC
const endTime = Date.UTC(year, month, day, 23, 59, 0, 0);
一行程序-考虑本地时区并且没有库
const todayStart = new Date(new Date().setHours(0, 0, 0, 0)) const todayEnd = new Date(new Date().setHours(23, 59, 59, 999)) const tomorrowStart = new Date(new Date(new Date().setHours(0, 0, 0, 0)).setDate(new Date().getDate() + 1)) const tomorrowEnd = new Date(new Date(new Date().setHours(23, 59, 59, 999)).setDate(new Date().getDate() + 1)) const monthStart = new Date(new Date(new Date().getFullYear(), new Date().getMonth(), 1).setHours(0, 0, 0, 0)) const monthEnd = new Date(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).setHours(23, 59, 59, 999)) const nextMonthStart = new Date(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 1).setHours(0, 0, 0, 0)) const nextMonthEnd = new Date(new Date(new Date().getFullYear(), new Date().getMonth() + 2, 0).setHours(23, 59, 59, 999)) console.log({ todayStart, todayEnd, tomorrowStart, tomorrowEnd, monthStart, monthEnd, nextMonthStart, nextMonthEnd, })
如果你对UTC时间的开始/结束感兴趣,你也可以使用模运算符:
const now = new Date().getTime();
let startOfDay = now - (now % 86400000);
let endDate = startOfDay + 86400000;
其中86400是一天的秒数,结果变量是Epoch(以毫秒为单位)。
如果你喜欢日期对象:
const now = new Date().getTime();
let startOfDay = new Date(now - (now % 86400000));
let endDate = new Date(now - (now % 86400000) + 86400000);
var start = new Date(); start.setUTCHours (0, 0, 0, 0); var end = new Date(); end.setUTCHours(23日,59,59999); alert(start.toUTCString() + ':' + end.toUTCString());
如果需要从中获取UTC时间,可以使用UTC()。