-3

I have created a simple javascript to add 5 days to the current date. I am now having issues getting it to display the format day, date month i.e. Tue 7th Nov. Please can someone help

var newDt = new Date();
newDt.setDate(newDt.getDate() + 5);
document.writeln("" + newDt);
  • 3
    Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – msanford Nov 07 '17 at 20:29
  • 1
    Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Sergey Sklyar Nov 07 '17 at 20:34

2 Answers2

0

Try out this. If you want it in the format of Weekday Month Day Year remove the .slice(0, -5); on date.

There is plenty of documentation online. You have to look.

Read more about toDateString() here.

Read more about .slice() here.

var newDt = new Date();
newDt.setDate(newDt.getDate() + 5);
var date = newDt.toDateString();
document.writeln("" + date.slice(0, -5)); 

To make it in the format you want, Weekday Day Month use this example.

var date = new Date();
var locale = "en-us";

var weekdayNumber = date.toLocaleString(locale, { weekday: "short"});
var calenderDay = date.getDate();

var month = date.toLocaleString(locale, { month: "short" });
document.writeln(weekdayNumber + " " + calenderDay + "th " + month); 

Be careful with dates like the 1st and 2nd or anything other than th

Jimenemex
  • 2,836
  • 2
  • 13
  • 39
  • That's great thanks. Still very new to JS. Just a quickie, how do I swap the month and date round and add a th, after the date? Can't see that on the links? – Hashtag CV Nov 07 '17 at 20:40
  • @HashtagCV Consider accepting my answer or others if it solved your problem, or upvote if it was helpful. – Jimenemex Nov 14 '17 at 21:34
0
newDt.toDateString() 

will return "Tue Nov 12 2017"

Alternatively, you can use a variety of date methods to build a date string that might be more amenable to your needs.

See date methods here:

https://www.w3schools.com/js/js_date_methods.asp