-2

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()?

bigdaveygeorge
  • 947
  • 2
  • 12
  • 32
  • `.getDate()` + `.setDate()` – Andreas May 11 '20 at 10:21
  • https://stackoverflow.com/search?q=%5Bjavascript%5D+add+days – freedomn-m May 11 '20 at 10:30
  • Does this answer your question? [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – freedomn-m May 11 '20 at 10:32
  • `.getDate()` gets the day of the month (an integer), adding 3 to this won't give you a full date https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate it's another example of javascript's really badly named methods – freedomn-m May 11 '20 at 10:33

1 Answers1

0

You can use getTime function it will provide your date into milliseconds. Add total milliseconds for 3 days (i.e. 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);
Karan
  • 12,059
  • 3
  • 24
  • 40