[英]generate date range base on integers
I have an array like this [0,1,2,3 .. 23]
and they means hour in a day. 我有一个像这样的数组
[0,1,2,3 .. 23]
,它们表示一天中的小时。 How to use moment js to convert them to 12 hour then do date range? 如何使用moment js将其转换为12小时,然后设置日期范围?
my desire output is 我的愿望输出是
['12am-1am','1am-2am','2am-3am' .. 11pm-12am]
my attempt failed coz I thought I don't need moment https://jsfiddle.net/s4L7hj1a 我的尝试失败了,因为我以为我不需要片刻https://jsfiddle.net/s4L7hj1a
You don't need moment.js 您不需要moment.js
var getFormattedHour = function(hour) { return (hour % 12 ? hour % 12 : 12) + ':00 ' + ((hour < 12) || (hour >= 24) ? 'AM' : 'PM'); } var getHourRange = function(hour) { return getFormattedHour(hour) + ' - ' + getFormattedHour(hour + 1); } var hours = []; for(var hour = 0; hour < 24; hour++) { hours.push(hour); } hours.map(function(hour) { console.log(getHourRange(hour)); });
Using moment.js: 使用moment.js:
const moment = require('moment');
const start = moment().startOf('day');
const times = 24; // 24 hours
for (let i = 0; i < times; i++) {
const toPrint = moment(start)
.add(60 * i, 'minutes')
.format('hh:mm A');
console.log(toPrint);
}
This will give yo: 这将给你:
12:00 AM
01:00 AM
02:00 AM
...
...
10:00 PM
11:00 PM
then you can add them in array as you like 然后您可以根据需要将它们添加到数组中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.