如何在时间戳(GMT)中开始(00:00:00)和结束(23:59:59)今天?电脑使用当地时间。


当前回答

一行程序-考虑本地时区并且没有库

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, })

其他回答

如果您只对GMT的时间戳感兴趣,也可以这样做,它可以方便地适应不同的间隔(小时:1000 * 60 * 60,12小时:1000 * 60 * 60 * 12,等等)。

const interval = 1000 * 60 * 60 * 24; // 24 hours in milliseconds

let startOfDay = Math.floor(Date.now() / interval) * interval;
let endOfDay = startOfDay + interval - 1; // 23:59:59:9999

我们可以用矩。

// for day start time
moment(moment().startOf('day')).format('HH:mm')

// for day end time
moment(moment().endOf('day')).format('HH:mm')

我更喜欢使用date-fns库进行日期操作。这是一个非常棒的模块化和一致的工具。你可以这样开始和结束一天:

var startOfDay = dateFns.startOfDay; var endOfDay = dateFns.endOfDay; console.log('start of day ==> ', startOfDay(new Date('2015-11-11'))); console.log('end of day ==> ', endOfDay(new Date('2015-11-11'))); < script src = " https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js " > < /脚本>

// 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, })