我试图在整个24小时内每隔X分钟创建一个时间数组(字符串,而不是Array
(
[Apple] => apple is a fruit
[Ball] => ball is used to play
[Cat] => cat is an animal
[Eagle] => eagle is a bird
)
个对象)。例如,对于5分钟的间隔,阵列将是:
Date
我的快速而肮脏的解决方案是使用3个嵌套的['12:00 AM', '12:05 AM', '12:10 AM', '12:15 AM', ..., '11:55 PM']
循环:
for
这会输出所需的结果,但我想知道是否有更优雅的解决方案。有没有办法做到这一点:
答案 0 :(得分:24)
如果间隔仅以分钟[0-60]设置,则评估以下解决方案,而不是创建日期对象并在单循环中:
var x = 5; //minutes interval
var times = []; // time array
var tt = 0; // start time
var ap = ['AM', 'PM']; // AM-PM
//loop to increment the time and push results in array
for (var i=0;tt<24*60; i++) {
var hh = Math.floor(tt/60); // getting hours of day in 0-24 format
var mm = (tt%60); // getting minutes of the hour in 0-55 format
times[i] = ("0" + (hh % 12)).slice(-2) + ':' + ("0" + mm).slice(-2) + ap[Math.floor(hh/12)]; // pushing data in array in [00:00 - 12:00 AM/PM format]
tt = tt + x;
}
console.log(times);
答案 1 :(得分:11)
分配结果数组以避免推送,参数验证和区域设置细节的开销,尽管如此:
function generate_series(step) {
var dt = new Date(1970, 0, 1, 0, 0, 0, 0),
rc = [];
while (dt.getDate() == 1) {
rc.push(dt.toLocaleTimeString('en-US'));
dt.setMinutes(dt.getMinutes() + step);
}
return rc;
}
答案 2 :(得分:8)
在Moment.js的帮助下,以下内容非常灵活。
此代码使用
没有错误处理,因此您可以传入愚蠢的参数,但它可以解决问题。 :-D
desiredStartTime
参数需要hh:mm
格式的时间。
period
参数接受任何moment.duration
输入。
const timelineLabels = (desiredStartTime, interval, period) => {
const periodsInADay = moment.duration(1, 'day').as(period);
const timeLabels = [];
const startTimeMoment = moment(desiredStartTime, 'hh:mm');
for (let i = 0; i <= periodsInADay; i += interval) {
startTimeMoment.add(i === 0 ? 0 : interval, period);
timeLabels.push(startTimeMoment.format('hh:mm A'));
}
return timeLabels;
};
/* A few examples */
const theDiv = document.getElementById("times");
let content;
content = JSON.stringify(timelineLabels('18:00', 2, 'hours'))
theDiv.appendChild(document.createTextNode(content));
theDiv.appendChild(document.createElement("p"));
content = JSON.stringify(timelineLabels('06:00', 30, 'm'))
theDiv.appendChild(document.createTextNode(content));
theDiv.appendChild(document.createElement("p"));
content = JSON.stringify(timelineLabels('00:00', 5, 'minutes'))
theDiv.appendChild(document.createTextNode(content));
theDiv.appendChild(document.createElement("p"));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<div id="times"></div>
&#13;
答案 3 :(得分:6)
如果您有权访问moment
,您可以随时执行以下操作:
const locale = 'en'; // or whatever you want...
const hours = [];
moment.locale(locale); // optional - can remove if you are only dealing with one locale
for(let hour = 0; hour < 24; hour++) {
hours.push(moment({ hour }).format('h:mm A'));
hours.push(
moment({
hour,
minute: 30
}).format('h:mm A')
);
}
结果是以下数组:
["12:00 AM", "12:30 AM", "1:00 AM", "1:30 AM", "2:00 AM", "2:30 AM", "3:00 AM", "3:30 AM", "4:00 AM", "4:30 AM", "5:00 AM", "5:30 AM", "6:00 AM", "6:30 AM", "7:00 AM", "7:30 AM", "8:00 AM", "8:30 AM", "9:00 AM", "9:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "1:00 PM", "1:30 PM", "2:00 PM", "2:30 PM", "3:00 PM", "3:30 PM", "4:00 PM", "4:30 PM", "5:00 PM", "5:30 PM", "6:00 PM", "6:30 PM", "7:00 PM", "7:30 PM", "8:00 PM", "8:30 PM", "9:00 PM", "9:30 PM", "10:00 PM", "10:30 PM", "11:00 PM", "11:30 PM"]
答案 4 :(得分:5)
您只需要一个循环,按照这种方法
var d = new Date(); //get a date object
d.setHours(0,0,0,0); //reassign it to today's midnight
现在继续添加5分钟,直到d.getDate()
值更改
var date = d.getDate();
var timeArr = [];
while ( date == d.getDate() )
{
var hours = d.getHours();
var minutes = d.getMinutes();
hours = hours == 0 ? 12: hours; //if it is 0, then make it 12
var ampm = "am";
ampm = hours > 12 ? "pm": "am";
hours = hours > 12 ? hours - 12: hours; //if more than 12, reduce 12 and set am/pm flag
hours = ( "0" + hours ).slice(-2); //pad with 0
minute = ( "0" + d.getMinutes() ).slice(-2); //pad with 0
timeArr.push( hours + ":" + minute + " " + ampm );
d.setMinutes( d.getMinutes() + 5); //increment by 5 minutes
}
答案 5 :(得分:3)
此代码段根据语言环境生成范围,因此您无需手动处理AM / PM。默认情况下,它将使用浏览器语言,但是您可以传递用户选择的语言。
{{1}}
答案 6 :(得分:2)
在这种情况下不需要循环。
//Array.from, only supported by Chrome 45+, Firefox 32+, Edge and Safari 9.0+
//create an array of the expected interval
let arr = Array.from({
length: 24 * 60 / 5
}, (v, i) => {
let h = Math.floor(i * 5 / 60);
let m = i * 5 - h * 60;
//convert to 12 hours time
//pad zero to minute
if (m < 10) {
m = '0' + m;
}
let label = 'AM';
if (h > 12) {
label = 'PM';
h -= 12;
}
if (h === 0) {
h = 12;
}
return h + ':' + m + ' ' + label;
});
document.body.textContent = JSON.stringify(arr);
&#13;
var arr = Array.apply(null, {
length: 24 * 60 / 5
}).map(function(v, i) {
var h = Math.floor(i * 5 / 60);
var m = i * 5 - h * 60;
if (m < 10) {
m = '0' + m;
}
var label = 'AM';
if (h > 12) {
label = 'PM';
h -= 12;
}
if (h === 0) {
h = 12;
}
return h + ':' + m + ' ' + label;
});
document.body.textContent = JSON.stringify(arr);
&#13;
答案 7 :(得分:2)
这是Faizan Saiyed的回答的迭代。
const startHour = moment().format('k');
const endHour = 22
const arr = () => {
return Array.from({
length: endHour - startHour
}, (v, index) => {
return [0,15,30,45].map((interval) => {
return moment({
hour: index,
minute: interval
})
.add(startHour, 'hours')
.format('h:mm A')
})
}).flat()
}
答案 8 :(得分:1)
您可以使用一个for
循环,while
循环,Array.prototype.map()
,Array.prototype.concat()
,String.prototype.replace()
var n = 0,
min = 5,
periods = [" AM", " PM"],
times = [],
hours = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
for (var i = 0; i < hours.length; i++) {
times.push(hours[i] + ":" + n + n + periods[0]);
while (n < 60 - min) {
times.push(hours[i] + ":" + ((n += 5) < 10 ? "O" + n : n) + periods[0])
}
n = 0;
}
times = times.concat(times.slice(0).map(function(time) {
return time.replace(periods[0], periods[1])
}));
console.log(times)
&#13;
答案 9 :(得分:1)
使用整数和使用单循环操作日期:
var interval = 5 * 60 * 1000; //5 minutes
var period = 24 * 60 * 60 * 1000; //dat period
//just converts any time to desired string
var toString = function toString(time){
var h = time.getHours();
var m = time.getMinutes();
var p = h >= 12 ? "PM" : "AM";
h = h || 12;
h = h > 12 ? h - 12 : h;
return ("0" + h).slice(-2) + ":" + ("0" + m).slice(-2) + " " + p;
}
//start time
var time = new Date(2010, 0, 1);
//resulting array
var times = [];
for ( var t = +time; t < +time + period; t += interval){
var d = toString(new Date(t));
times.push(d);
}
答案 10 :(得分:1)
在任何情况下,您都需要执行O(N)操作来枚举数组元素。
但是,您可以遍历Date
个对象本身。
function timeArr(interval) //required function with custom MINUTES interval
{
var result = [];
var start = new Date(1,1,1,0,0);
var end = new Date(1,1,2,0,0);
for (var d = start; d < end; d.setMinutes(d.getMinutes() + 5)) {
result.push(format(d));
}
return result;
}
function format(inputDate) // auxiliary function to format Date object
{
var hours = inputDate.getHours();
var minutes = inputDate.getMinutes();
var ampm = hours < 12? "AM" : (hours=hours%12,"PM");
hours = hours == 0? 12 : hours < 10? ("0" + hours) : hours;
minutes = minutes < 10 ? ("0" + minutes) : minutes;
return hours + ":" + minutes + " " + ampm;
}
答案 11 :(得分:1)
我的解决方案强调可读性。它首先创建表示正确时间的对象,然后将它们格式化为字符串。 JsFiddle https://jsfiddle.net/6qk60hxs/
var periods = ['AM', 'PM'];
var hours = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
var minutes = ["00", "05", 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
timeObj = add([{}], "p", periods);
timeObj = add(timeObj, "h", hours);
timeObj = add(timeObj, "m", minutes);
times = []
for (t of timeObj) {
times.push(t.h + ':' + t.m + ' ' + t.p);
}
console.log(times)
function add(tab, prop, val) {
var result = [];
for (t of tab) {
for (v of val) {
tc = _.clone(t);
tc[prop] = v;
result.push(tc);
}
}
return result
}
答案 12 :(得分:0)
fileprivate func addBarButton() {
if tableView.isEditing {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(editButtonAction))
} else {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(doneButtonAction))
}
}
@objc func editButtonAction(sender: UIBarButtonItem) {
tableView.isEditing = !tableView.isEditing
}
@objc func doneButtonAction(sender: UIBarButtonItem) {
tableView.isEditing = !tableView.isEditing
}
override func viewDidLoad() {
super.viewDidLoad()
addBarButton()
tableView?.isEditing = true
}
答案 13 :(得分:0)
此操作的结果是将数组times
转换为标记,并将其放置在<select>
对象中。我发现underscore.js's _.range允许我逐步执行增量操作,但只能是整数。因此,我使用moment.js转换为Unix时间。我需要遍历几分钟slottime
,但是乘数可以完成其他间隔,在我的情况下,60000
再次将分钟添加到valueOf()
。
function slotSelect(blockstart, blockend, slottime) {
var markup = "";
var secs = parseInt(slottime * 60000); // steps
var a = parseInt( moment(blockstart).valueOf() ); // start
var b = parseInt( moment(blockend).valueOf() );
var times = _.range(a, b, secs);
_.find( times, function( item ) {
var appttime = moment(item).format('h:mm a');
var apptunix = moment(item).format();
markup += '<option value="'+apptunix+'"> ' + appttime + ' </option>'+"\n";
});
return markup;
}
答案 14 :(得分:0)
@section Scripts {
<script src="~/lib/@@AspNetCore/signalr/dist/browser/signalr.js"></script>
<script src="~/js/chat.js"></script>
}
答案 15 :(得分:0)
要从特定时间(例如3:00 PM)开始列表并每季度循环一次(每15分钟):
const hours = [];
const startHour = 15;
for (let hour = 0; hour < 24; hour++) {
hours.push(
moment({ hour })
.add(startHour, 'hours')
.format('h:mm A')
);
hours.push(
moment({
hour,
minute: 15
})
.add(startHour, 'hours')
.format('h:mm A')
);
hours.push(
moment({
hour,
minute: 30
})
.add(startHour, 'hours')
.format('h:mm A')
);
hours.push(
moment({
hour,
minute: 45
})
.add(startHour, 'hours')
.format('h:mm A')
);
}