Murphy1976
Murphy1976

Reputation: 1475

Moment.js - Getting Tomorrow and displaying Its day of the week

I'm using Moment.js to display time and dates on a site I am working on.

I have a Weather Module that displays TODAY, but I also want to display the forecast for 3 Days forward. I can use Moment.js to get Tomorrow, Tomorrow + 1 and Tomorrow + 2, but I don't know how to change their format so that it reads "WED, THU, FRI". Here's what I have currently:

var today = moment().format('MMMM D, YYYY');
var d1 = moment().add(1, 'days').calendar().format('ddd');
var d2 = moment().add(2, 'days').calendar().format('ddd');
var d3 = moment().add(3, 'days').calendar().format('ddd');

The error I get is that moment(...).add(...).calendar(...).format is not a funtion. If I use THIS coode:

var d1 = moment().add(1, 'days'); and then console.log() that variable, I can SEE: M {_isAMomentObject: true, _isUTC: false, _pf: {…}, _locale: P, _d: Thu Nov 28 2019 08:51:46 GMT-0600 (Central Standard Time), …} So I KNOW it's available, I just don't know the proper syntax to display the three character label for the day of the week.

Upvotes: 0

Views: 121

Answers (1)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

Remove the .calendar() call, I am not sure why you even need that.

var today = moment();
var d0 = today.format('ddd').toUpperCase();
var d1 = moment(today).add(1, 'days').format('ddd').toUpperCase();
var d2 = moment(today).add(2, 'days').format('ddd').toUpperCase();
var d3 = moment(today).add(3, 'days').format('ddd').toUpperCase();

console.log(d0, d1, d2, d3);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Upvotes: 1

Related Questions