Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add and subtract 3 month from a given date time

I have this start date : 2023-09-03T00:00:00+05:30
and end date : 2023-09-10T00:00:00+05:30

I want to subtract 90 days from start date and add 90 days from end date

and then convert it to UTC format

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for that I was using this function in angular

moment(startStr, 'YYYY-MM-DD hh:mm:ss').utc().format('YYYY-MM-DD hh:mm:ss');
moment(endStr, 'YYYY-MM-DD hh:mm:ss').utc().format('YYYY-MM-DD hh:mm:ss');

How can I do that?

>Solution :

For moment you can use add and substract functions. Example below:

var startDate = moment(startStr, 'YYYY-MM-DD hh:mm:ss');
var endDate = moment(endStr, 'YYYY-MM-DD hh:mm:ss');

startDate =  startDate.subtract(90, 'days');
console.log(startDate);
endDate =  endDate.add(90, 'days');
console.log(endDate);

// then do your utc conversion

Reference material:

moment add function link

moment subsctract function link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading