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

250
Views
Comprobando si la fecha ingresada es el inicio de un trimestre

¡Buenas tardes! ¿Puede decirme cómo implementar una función que verifique si la fecha pasada es el comienzo del trimestre? Traté de implementar la validación usando la biblioteca moment.js y creé una función

 const isStartOfQuarter = (date: string) => { return moment(date).isSame(moment().quarter(moment(date).quarter()).startOf('quarter'), 'day') }

Al llamar a esta función con la fecha "2022-04-01" devuelve verdadero y funciona correctamente, pero si intentas llamarla con la fecha "2023-04-01" no funciona correctamente y devuelve falso

También necesito hacer una función que verifique si la fecha pasada es el final del trimestre Creé una función para esto

 const isEndOfQuarter = (date: string) => { return moment(date).isSame(moment().quarter(moment(date).quarter()).endOf('quarter'), 'day') }

Al llamar a esta función con la fecha "2022-06-30" devuelve verdadero y funciona correctamente, pero si intentas llamarla con la fecha "2023-06-30" no funciona correctamente y devuelve falso

¿Puede decirme cómo implementar esta funcionalidad correctamente?

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

0

No sugeriría usar momentjs. Incluso los autores recomiendan utilizar alternativas para nuevos desarrollos.

En este caso, es bastante fácil hacerlo con JavaScript nativo, ya que no hay tantos trimestres después de todo:

 const isStartOfQuarter = (date) => ["01-01", "04-01", "07-01", "10-01"].includes(date.slice(5, 10)); const isEndOfQuarter = (date) => ["03-31", "06-30", "09-30", "12-31"].includes(date.slice(5, 10)); console.log(isStartOfQuarter("2023-04-01"));

about 4 years ago · Juan Pablo Isaza Report

0

Simplemente usaría el tipo de Date incorporado.

 const isStartOfQuarter = (dateStr) => { const d = new Date(dateStr); return d.getUTCDate()==1 && d.getUTCMonth()%3==0 } const isEndOfQuarter = (dateStr) => { let d = new Date(dateStr); d.setDate(d.getDate()+1); return isStartOfQuarter(d); }

Que funcionan como se anuncia en Node:

 > isStartOfQuarter("2023-04-01") true > isEndOfQuarter("2023-06-30") true
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!