Reputation: 111
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;
Upvotes: 0
Views: 56
Reputation: 64657
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));
Upvotes: 4
Reputation: 1573
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"
Upvotes: 1