How to subtract days from current date

How can I subtract days from current …new Date ()

New Date() minus 7 days

e.g
Fri Aug 06 2021 11:29:57 GMT+0100 (BST ) - 7 days = Fri Jul 30 2021 11:29:57 GMT+0100 (BST )

Any ideas or pointers appreciated.I seem to be making it over complicated and does not handle year change either. It seems harder than I appreciated to keep it simple.

Hi @coz ,

You can do that like this:

const now = new Date();
const days7_before_now = new Date();

days7_before_now.setDate(now.getDate() - 7);

Hope this helps~!
Ahmad

Hi Ahmad

The only saving face I have thank goodness you cannot see my code. That’s a superb solution thank you. Glaringly simple when shown but I would not have thought of that, I was indeed over complicating the task. Thank you for such a fast reply, your time and knowledge is truly appreciated.

It helps very much indeed.
Coz

@coz you’re very much welcome :blush: Happy to help

Hi Everyone,

Please help me. I have been struggling with this for months !
I am storing this code in the data.js (Backend)

I want to get the number of days between the createdDate and Today. This is my code but it is not giving me anything in the item.days field in the collection.

export function Tasks_afterQuery ( item , context ) {

const createDate = item._createdDate ;
var date1 = new Date ( createDate );
var date2 = new Date ();

var timeDiff = Math . abs ( date2 . getTime ()) - Math . abs ( date1 . getTime ()); // get time diff in milliseconds
var diffDays = Math . ceil ( timeDiff / ( 1000 * 3600 * 24 )); // convert milliseconds to days

item.days = diffDays

return item ;
}

Any help is so gratefully appreciated.