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?

3

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.