获取基于时间间隔的字符串数组



我有两个日期时间,例如:

"start": "2021-04-10T10:05:00+02:00"
"end": "2021-04-10T11:35:00+02:00"

我想从这里得到的是一个基于两个日期之间持续时间的字符串数组,例如四分之一间隔。所以基本上上面的结果应该是:

var timeArray = ["10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30"]

我知道我可以从date-fns包中的intervalToDuration函数获得日期之间的时间持续时间,但我不确定如何从中创建这些时间戳。

有什么想法吗?

function getTimeArray(int, d1, d2){
let d1_ = new Date(d1);
let d2_ = new Date(d2);
let min = d1_.getMinutes();
let m = (min>=0&&min<=14)?0:(min>14&&min<=29)?15:(min>29&&min<=44)?30:(min>44&&min<=59)?45:0;
d1_.setMinutes(m);
let arr = [];
do {
let gh = ("0"+d1_.getHours()).slice(-2);
let gm = ("0"+d1_.getMinutes()).slice(-2);
arr.push(`${gh}:${gm}`);
d1_.setMinutes(d1_.getMinutes()+int);

} while (d1_ <= d2_);
return arr;
}
let arr = getTimeArray(15, "2021-04-10T10:05:00+02:00", "2021-04-10T11:35:00+02:00");
console.log(arr);

我会这样分解它,然后从开始时间迭代到结束时间,将每25小时推入数组。

var start = "2021-04-10T10:05:00+02:00"
start = start.split("T")[1].split("+")[0]
startHour = start.split(":")[0]
startMin = start.split(":")[1]

var end = "2021-04-10T11:35:00+02:00"
end = end.split("T")[1].split("+")[0]
endHour = end.split(":")[0]
endMin = end.split(":")[1]

最近我遇到了同样的情况,得到了以下解决方案:

让我们用时间而不是DateTime。

timeslot将是主要函数,它有三个参数timeIntervalstartTimeendTime

timeInterval => in minutes
startTime => min 00:00 and max 24:00
endTime => min 00:00 and max 24:00

const timeslots = (timeInterval, startTime, endTime) => {
// get the total minutes between the start and end times.
var totalMins = subtractTimes(startTime, endTime);
// set the initial timeSlots array to just the start time
var timeSlots = [startTime];
// get the rest of the time slots.
return getTimeSlots(timeInterval, totalMins, timeSlots);
}
// Generate an array of timeSlots based on timeInterval and totalMins
const getTimeSlots = (timeInterval, totalMins, timeSlots) => {
// base case - there are still more minutes
if (totalMins - timeInterval >= 0) {
// get the previous time slot to add interval to
var prevTimeSlot = timeSlots[timeSlots.length - 1];
// add timeInterval to previousTimeSlot to get nextTimeSlot
var nextTimeSlot = addMinsToTime(timeInterval, prevTimeSlot);
timeSlots.push(nextTimeSlot);
// update totalMins
totalMins -= timeInterval;
// get next time slot
return getTimeSlots(timeInterval, totalMins, timeSlots);
} else {
// all done!
return timeSlots;
}
}
// Returns the total minutes between 2 time slots
const subtractTimes = (t2, t1) => {
// get each time's hour and min values
var [t1Hrs, t1Mins] = getHoursAndMinsFromTime(t1);
var [t2Hrs, t2Mins] = getHoursAndMinsFromTime(t2);
// time arithmetic (subtraction)
if (t1Mins < t2Mins) {
t1Hrs--;
t1Mins += 60;
}
var mins = t1Mins - t2Mins;
var hrs = t1Hrs - t2Hrs;
// this handles scenarios where the startTime > endTime
if (hrs < 0) {
hrs += 24;
}
return (hrs * 60) + mins;
}
// Gets the hours and minutes as intergers from a time string
const getHoursAndMinsFromTime = (time) => {
return time.split(':').map(function (str) {
return parseInt(str);
});
}
// Adds minutes to a time slot.
const addMinsToTime = (mins, time) => {
// get the times hour and min value
var [timeHrs, timeMins] = getHoursAndMinsFromTime(time);
// time arithmetic (addition)
if (timeMins + mins >= 60) {
var addedHrs = parseInt((timeMins + mins) / 60);
timeMins = (timeMins + mins) % 60
if (timeHrs + addedHrs > 23) {
timeHrs = (timeHrs + addedHrs) % 24;
} else {
timeHrs += addedHrs;
}
} else {
timeMins += mins;
}
// make sure the time slots are padded correctly
return String("00" + timeHrs).slice(-2) + ":" + String("00" + timeMins).slice(-2);
}

最新更新