[英]Using moment js to create an array with days of the week and hours of the day?
Wondering if there was a way to get momentjs or just use pure javascript to create an array of everyday of the week, and every hour in a day so that I dont have to hardcode it. 想知道是否有办法获得momentjs或只是使用纯javascript来创建一周中的每天的数组,以及一天中的每个小时,以便我不必对其进行硬编码。
So instead of manually doing 所以不要手动做
weekArray = ["Monday", "Tuesday", "Wednesday" ....]
I'm looking for a way to do something like 我正在寻找一种方法来做类似的事情
weekArray = moment.js(week)
The same idea for times during the day especially, so I could potentially use different formats. 特别是在白天的时间相同的想法,所以我可能使用不同的格式。
对于工作日,您可以使用时刻的工作日方法
weekArray = moment.weekdays()
I use this solution: 我使用这个解决方案:
var defaultWeekdays = Array.apply(null, Array(7)).map(function (_, i) {
return moment(i, 'e').startOf('week').isoWeekday(i + 1).format('ddd');
});
I got result: 我得到了结果:
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
You can Modify .format(string) to change the days format. 您可以修改.format(字符串)以更改日期格式。 Eg 'dddd' will shows:
例如'dddd'将显示:
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
Check Moment.js documentation for more advanced format 检查Moment.js文档以获取更高级的格式
Here's a little snippet to get the (locale-specific) names of the days of the week from Moment.js: 这里有一个小片段来获取Moment.js中一周中某些日期的(特定于语言环境)名称:
var weekdayNames = Array.apply(null, Array(7)).map(
function (_, i) {
return moment(i, 'e').format('dddd');
});
console.log(weekdayNames);
// Array [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
If you want the week to start on Monday, replace moment(i, 'e')
with moment(i+1, 'e')
. 如果您希望星期一开始,请用
moment(i+1, 'e')
替换moment(i, 'e')
moment(i+1, 'e')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.