• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

230
Views
Obtenga una matriz de cadenas en función de un intervalo de tiempo

Tengo dos fechas y horas, por ejemplo:

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

Lo que me gustaría obtener de esto es una serie de cadenas basadas en la duración entre las dos fechas, por ejemplo, en un intervalo de un cuarto. Entonces, básicamente, lo anterior debería resultar en:

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

Sé que puedo obtener la duración del tiempo entre las fechas de la función intervalToDuration en el paquete date-fns , pero no estoy seguro de cómo crear estas marcas de tiempo a partir de eso.

Algunas ideas ?

almost 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

 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);

almost 3 years ago · Juan Pablo Isaza Report

0

Lo desglosaría así, luego iteraría desde el principio hasta el final, empujando cada cuarto de hora en la matriz.

 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]
almost 3 years ago · Juan Pablo Isaza Report

0

Recientemente me encontré con el mismo caso y obtuve la siguiente solución.

Juguemos con la hora en lugar del DateTime.

timeslot será la función principal y toma tres argumentos timeInterval , startTime y endTime .

 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); }
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error