0

For a project I'm working on I need a five day forecast. I'm starting out with tomorrow (which should be tomorrows date of whenever the user opens the app) and the date is appearing on the screen but it is formatted like this (Fri Jul 24 2020 22:40:10 GMT-0700 (Pacific Daylight Time)). What I would like is a format like this: Fri Jul 24.

Here is my javascript:

const tomorrow = new Date().format(ll) 

tomorrow.setDate(new Date().getDate() + 1);

one = document.getElementById('one');
one.innerHTML = tomorrow;
Aiya Siddig
  • 111
  • 9
  • Are you using any library like `moment`? – Karan Jul 24 '20 at 06:06
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – jdaz Jul 24 '20 at 07:03

2 Answers2

4

I would just use toLocaleDateString with the right options:

const formatDate = (d) => {
  const options = { weekday: 'short', month: 'short', day: 'numeric' };
  return d.toLocaleDateString("en-US", options).replace(',','');
}
const tomorrow = new Date()
tomorrow.setDate(new Date().getDate() + 1);
console.log(formatDate(tomorrow));
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(formatDate(tomorrow));
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(formatDate(tomorrow));
dave
  • 62,300
  • 5
  • 72
  • 93
1

By using .toDateString() function you can get return of Fri Jul 24 2020.

Then you can delete the last 4 characters using substr.

Here is a simple example:

const tomorrow = new Date()
tomorrow.setDate(new Date().getDate() + 1)

console.log(tomorrow.toDateString())
// "Sat Jul 25 2020"
console.log(tomorrow.toDateString().substr(0, 10))
// "Sat Jul 25"
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nurul Huda
  • 1,438
  • 14
  • 12