Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
Dada una fecha ISO, obtenga los días de la semana en forma de rangos de cadenas ISO

Supongamos que tengo una fecha ISO, como 2021-09-18T20:18:27.000Z

  1. Me gustaría obtener las fechas de la semana pasada en forma de matriz
  2. La matriz debe ser un conjunto de matrices que tengan dos fechas ISO que representen el inicio y el final del día, como se ilustra a continuación:

P.ej

aporte:

 2021-09-18T20:18:27.000Z

producción:

 [ ['"2021-09-11T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'], ['"2021-09-12T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'], ['"2021-09-13T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'], ['"2021-09-14T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'], ['"2021-09-15T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'], ['"2021-09-16T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'], ['"2021-09-17T00:00:00.000Z', '"2021-09-11T23:59:59.000Z'], ['"2021-09-18T00:00:00.000Z', '"2021-09-18T23:59:59.000Z'], ]

Ya lo probé con dayjs, esto da como resultado que la matriz represente intervalos exactos desde una fecha en particular:

 function getDates(date) { var dates = [date] var noOfDays = 7 for (let i = noOfDays - 1; i >= 0; i--) { const elementIndex = i - noOfDays // Get last nth element of the list const dateToIncrement = dates.slice(elementIndex)[0] const newDate = dayjs(dateToIncrement).subtract(1, "day").toISOString() dates.unshift(newDate) } return dates }

Gracias

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

function getPastWeek(inputTime) { var res = []; //result array var currentDayEnd = undefined; //variable to be set on each iteration var currentDay = new Date(inputTime.split('T')[0]); //create new Date object currentDay.setDate(currentDay.getDate() - 7); //reduce seven days from current date for(var i = 0; i <= 7; i++) { //foreach day in last week currentDayEnd = new Date(currentDay.getTime() - 1000); //previous day end (1sec before current day start) currentDayEnd.setDate(currentDayEnd.getDate() + 1); //current day end (one day after previous day end) res.push([currentDay.toISOString(), currentDayEnd.toISOString()]); //append pair currentDay.setDate(currentDay.getDate() + 1); //set variable to next day } return res; } var pastWeek = getPastWeek('2021-09-18T20:18:27.000Z'); //call example
about 4 years ago · Juan Pablo Isaza Report

0

El código OP está bastante cerca, deberías haber podido adaptarlo a tus necesidades. Lo siguiente emplea un algoritmo similar. El final del día se establece en 1 ms antes de la medianoche.

 // Given a UTC ISO 8601 timestamp, return array of day plus // previous 6 days with start of day, end of day timestmaps function getDates(d) { let s = new Date(d.slice(0,10)); let e = new Date(+s); e.setUTCHours(23,59,59,999); let result = []; for (let i=7; i; i--) { result.push([s.toISOString(), e.toISOString()]); s.setUTCDate(s.getUTCDate() - 1); e.setUTCDate(e.getUTCDate() - 1); } return result; } console.log(getDates(new Date().toISOString()));

about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!