问题:我需要创建两个日期/时间戳(日期格式:2016-08-10 16:00:00)之间所有15分钟时隙的数组,形成HH:mm格式,其中分钟受限制到00,15,30,45。
示例:下午12:30至下午2:30将是> 12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15
下面的代码正在运行,但它没有考虑到起始时间段(它总是从' 00'开始)。代码已经过调整以适应它,但现在它不能正确地增加小时数。
function generateTimeSlots(s,e)
{ // create an array of timeslots (15 min interval) between 2 dates
var ckTimes = new Array();
var timeSlots = new Array();
var diff = Math.abs((new Date(e)) - (new Date(s)));
var duration = Math.floor((diff/1000)/60);
var sHour = (((s.split(' ')[1]).split(':'))[0]) * 1;
var ampm = (s.split(' '))[2];
// convert hour to 24 hour
if(ampm+'' == 'PM' ${AND} sHour != 12)
{
sHour = sHour + 12;
}
var hours = (duration / 60);
var slots = (hours * 4); // total 15 min slots
var chunk = ['00','15','30','45'] // the four 15 min slots in a hour
// Attempt to move starting position to the correct 15 min block
// Beforehand, it would always start at the '00' slot
// i.e. 12:30 pm to 1:30pm would return 12:00,12:15,12:30,12:45
var i = "";
var startMin = (((s.split(' ')[1]).split(':'))[1])
// note platform doesn't support indexOf
for (var z = 0; 4 > z; z++){
if (chunk[z].toString() == startMin.toString())
i = z;
}
slots += i;
for(i;slots > i;i++)
{
var quarter = (i % 4);
var sMin = chunk[quarter];
var slot = sHour+":"+sMin;
if(ampm+'' == 'AM' ${AND} 10 > sHour)
{
slot = "0"+slot;
}
timeSlots.push(slot)
if(hours > 1)
{
if(quarter == 3)
{
sHour++;
}
}
}
return timeSlots;
}
答案 0 :(得分:15)
我可以建议moment.js
吗?它使这样的任务变得更加容易:
function intervals(startString, endString) {
var start = moment(startString, 'YYYY-MM-DD hh:mm a');
var end = moment(endString, 'YYYY-MM-DD hh:mm a');
// round starting minutes up to nearest 15 (12 --> 15, 17 --> 30)
// note that 59 will round up to 60, and moment.js handles that correctly
start.minutes(Math.ceil(start.minutes() / 15) * 15);
var result = [];
var current = moment(start);
while (current <= end) {
result.push(current.format('YYYY-MM-DD HH:mm'));
current.add(15, 'minutes');
}
return result;
}
console.log(intervals('2016-08-10 4:35:00 PM', '2016-08-10 8:06:00 PM'));
// Output:
// [ '2016-08-10 16:45',
// '2016-08-10 17:00',
// '2016-08-10 17:15',
// '2016-08-10 17:30',
// '2016-08-10 17:45',
// '2016-08-10 18:00',
// '2016-08-10 18:15',
// '2016-08-10 18:30',
// '2016-08-10 18:45',
// '2016-08-10 19:00',
// '2016-08-10 19:15',
// '2016-08-10 19:30',
// '2016-08-10 19:45',
// '2016-08-10 20:00' ]
答案 1 :(得分:1)
JS版本。
https://jsfiddle.net/justindevs/wgk63o1y/
//Input
var startTime = "2016-08-10 02:30:00"
var endTime = "2016-08-10 04:45:00"
//Parse In
var parseIn = function(date_time){
var d = new Date();
d.setHours(date_time.substring(11,13));
d.setMinutes(date_time.substring(14,16));
return d;
}
//make list
var getTimeIntervals = function (time1, time2) {
var arr = [];
while(time1 < time2){
arr.push(time1.toTimeString().substring(0,5));
time1.setMinutes(time1.getMinutes() + 15);
}
return arr;
}
startTime = parseIn(startTime);
endTime = parseIn(endTime);
var intervals = getTimeIntervals(startTime, endTime);