简体   繁体   中英

How to add x days to a date returned from datepicker's getDate() and return in specific date format?

I am looking to add some days to a date returned by a jQuery datepicker field and then put it in an easy to read date format.

I have this code:

var end = $("#to-date").datepicker("getDate");

Returns:

Tue May 12 2020 00:00:00 GMT+0100 (British Summer Time)

I want to add 3 days to that date and then get that date returned in another date format.

I add the extra days:

console.log( end.getDate() + 3 );

Returns:

15 (3 days from the 12th)

I was expecting this to be

Fri May 15 2020 00:00:00 GMT+0100 (British Summer Time)

The end result I am looking for is to get the date in a dd/mm/yy format after adding days to an existing date.

Is this possible with getDate() ?

You can use getTime function it will provide your date into milliseconds. Add total milliseconds for 3 days (ie 3 * 24 * 60 * 60 * 1000) into this getTime value and convert into date again.

Test it below.

 let start = new Date(); console.log(start); let end = new Date(start.getTime() + (3 * 24 * 60 * 60 * 1000)); console.log(end);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM