I am trying to get 7 days back epoch 12 am time but I am getting 7 days back time but same as the time at present like at 5 pm it gives 7 days back but at 5pm istead of 12 am. below code is what I tried:
let createdAt = new Date();
createdAt = new Date().setDate(createdAt.getDate()-7);
please help me to understand , how to get 12 am of 7 days back?, thanks in advance.
You need to zero the time too, e.g.
let createdAt = new Date();
createdAt.setHours(0,0,0,0);
createdAt = new Date().setDate(createdAt.getDate()-7);
The last line could also be:
createdAt = createdAt.setDate(createdAt.getDate() - 7);
since the set methods return the time value of the updated date.
Or create a new Date from just the date parts and get its time value, e.g.
let d = new Date();
let ms = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 7).getTime();
console.log(ms + '\n' + new Date(ms).toString());