Home > Net >  How to create date object and set date in 1 line in typescript?
How to create date object and set date in 1 line in typescript?

Time:09-19

Hi I have 2 lines of code, 1 creating date object, 1 set new date to yesterday.

yesterday = new Date () ;
this.yesterday.setDate(this.yesterday.getDate()-1);

How can I simplfy the code and do the work in a single line?

Something like this

yesterday = new Date ().setDate(this.yesterday.getDate()-1);

Or this

yesterday = new Date ()=>{setDate(this.yesterday.getDate()-1)}; 

Is it even possible? Thanks in advance :)

CodePudding user response:

It is Possible

const yesterday = new Date(new Date().setDate(new Date().getDate() - 1))

Or

const yesterday = new Date(Date.now() - 864e5); // 864e5 == 86400000 == 24*60*60*1000
  • Related