0

In JavaScript, I want to show today's date plus 3 days.

I can do this with the following code:

    var newDt = new Date();
    newDt.setDate(newDt.getDate()+2);
    document.writeln(newDt);       

This works well and outputs:

Thu Jan 03 2019 18:39:00 GMT+0000 (Greenwich Mean Time)

I, however would like to format the date as "Day, Month, Year" without any additional time included.

I know there are various libraries that can do this, but is there a way to do it without?

AndrewL64
  • 13,967
  • 6
  • 36
  • 64
Adam Scot
  • 1,073
  • 2
  • 14
  • 33
  • 1
    Duplicate https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – Henry Jan 01 '19 at 19:02
  • The lazy approach would be something like moment.js – Sirko Jan 01 '19 at 19:03
  • 1
    Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – FZs Jan 01 '19 at 19:19

2 Answers2

4

Just use the toLocaleDateString() method and specify en-GB as the locale to return the date in the British English date format of day-month-year order like this:

var x = document.getElementById("date");

var y  = new Date();
y.setDate(y.getDate() + 3);

var z = y.toLocaleDateString("en-GB");

x.innerHTML = `The date is: ${z}`;
<div id="date"></div>

You can check the toLocaleDateString documentation here() or check this answer on another SO thread to see how you can add options to further customise the date format.

AndrewL64
  • 13,967
  • 6
  • 36
  • 64
1

There is no format string. The only way is to pull the data points one by one

date.getDate() + ', ' + (date.getMonth() + 1) + ', ' +  date.getFullYear()
Leland Barton
  • 2,884
  • 17
  • 17