creativated
creativated

Reputation: 211

Get yesterday's date and set time to 01:30:00

I am trying to get the value of yesterday in ISOString and make sure time is showing 01:30:00

I am trying the following code:

const today = new Date()
const yesterday = new Date(today)

yesterday.setDate(yesterday.getDate() - 1)
yesterday.setHours(1,30,0)
console.log(yesterday)
x = today.toISOString()
y = yesterday.toISOString()

This is not giving the correct value in UST format.

For Example, if the date today is 2020-04-21T00:07:43.663Z then I want to get 2020-04-20T01:30:00.000Z as result in a variable.

Any Advice?

Upvotes: 0

Views: 83

Answers (1)

StackSlave
StackSlave

Reputation: 10627

Not sure what you really want. Yesterday at 1:30 am is not the same in Greenwich Mean Time (UTC).

const yesterday = new Date(Date.now()-86400000);
console.log(yesterday.toString());
yesterday.setHours(1, 30, 0, 0);
console.log(yesterday.toString());
console.log(yesterday.toISOString());
yesterday.setUTCHours(1, 30, 0, 0);
console.log('------------------------------');
console.log(yesterday.toISOString());

Upvotes: 2

Related Questions