[英]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.我正在使用 Moment.js 在我工作的网站上显示时间和日期。
I have a Weather Module that displays TODAY, but I also want to display the forecast for 3 Days forward.我有一个显示今天的天气模块,但我还想显示未来 3 天的预报。 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".我可以使用 Moment.js 获取 Tomorrow、Tomorrow + 1 和 Tomorrow + 2,但我不知道如何更改它们的格式,使其显示为“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.我得到的错误是moment(...).add(...).calendar(...).format
不是函数。 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.然后是console.log()
那个变量,我可以看到:M { _isAMomentObject M {_isAMomentObject: true, _isUTC: false, _pf: {…}, _locale: P, _d: Thu Nov 28 2019 08:51:46 GMT-0600 (Central Standard Time), …}
所以我知道它可用,我只是不知道显示星期几的三个字符标签的正确语法。
Remove the .calendar()
call, I am not sure why you even need that.删除.calendar()
调用,我不确定你为什么需要它。
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>
代替.calendar()...试试.day()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.