Home > database >  Epoch Value: How to set the time 12AM next day?
Epoch Value: How to set the time 12AM next day?

Time:11-02

Let's say I have an epoch value 1665531785000 which converts to "Tuesday, October 11, 2022 11:43:05 PM" in human readable format.

How can we modify 1665531785000 to 1665532800000 which converts to "Wednesday, October 12, 2022 12:00:00 AM"(set the value to 12AM next day) in javascript/typescript

CodePudding user response:

Doing complex date math is something you probably want to use a library for. There are a handful of options, but for example luxon is a library that could be used for this:

import { DateTime } from 'luxon';
DateTime.fromJSDate(new Date(1665531785000))
  .plus({ day: 1 })
  .startOf('day')
  .toJSDate();

CodePudding user response:

As of your example, I assume you would like to use UTC. You can do it without any additional libraries as below.

let d = new Date(1665531785000);
d.setUTCDate(d.getUTCDate()   1);
d.setUTCHours(0, 0, 0);
const e = d.valueOf();

console.log(e);
  • Related