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

701
Views
moment.js - prueba si una fecha es hoy, ayer, dentro de una semana o dos semanas

Estoy intentando con moment.js saber si una fecha es hoy, ayer, hace 1 semana o más (hace 2 semanas o más).

Ya lo hice para los dos primeros casos:

 var today = moment().startOf('day'); var yesterday = moment().subtract(1, 'days').startOf('day'); if (moment(localTime).isSame(today, 'd')) // today // do something if (moment(localTime).isSame(yesterday, 'd')) // yesterday // do something

¿Es eso correcto?

Sin embargo, ¿cómo puedo verificar si una fecha es de hace una semana o más (por ejemplo, hace dos semanas)?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Aquí hay algo que puede ser útil:

 var REFERENCE = moment("2015-06-05"); // fixed just for testing, use moment(); var TODAY = REFERENCE.clone().startOf('day'); var YESTERDAY = REFERENCE.clone().subtract(1, 'days').startOf('day'); var A_WEEK_OLD = REFERENCE.clone().subtract(7, 'days').startOf('day'); function isToday(momentDate) { return momentDate.isSame(TODAY, 'd'); } function isYesterday(momentDate) { return momentDate.isSame(YESTERDAY, 'd'); } function isWithinAWeek(momentDate) { return momentDate.isAfter(A_WEEK_OLD); } function isTwoWeeksOrMore(momentDate) { return !isWithinAWeek(momentDate); } console.log("is it today? ..................Should be true: "+isToday(moment("2015-06-05"))); console.log("is it yesterday? ..............Should be true: "+isYesterday(moment("2015-06-04"))); console.log("is it within a week? ..........Should be true: "+isWithinAWeek(moment("2015-06-03"))); console.log("is it within a week? ..........Should be false: "+isWithinAWeek(moment("2015-05-29"))); console.log("is it two weeks older or more? Should be false: "+isTwoWeeksOrMore(moment("2015-05-30"))); console.log("is it two weeks older or more? Should be true: "+isTwoWeeksOrMore(moment("2015-05-29")));

Verifique una demostración de JSFiddle con más pruebas, para que pueda ajustar su caso exacto, si es necesario.

over 4 years ago · Santiago Trujillo Report

0

Respuesta más precisa de la siguiente manera

 var today = moment(); var yesterday = moment().subtract(1, 'day'); var engagementDate = (Date to be compare); if(moment(engagementDate).isSame(today, 'day')) console.log('Today'); else if(moment(engagementDate).isSame(yesterday, 'day')) console.log('Yesterday');
over 4 years ago · Santiago Trujillo Report

0

La solución más simple para hoy:

 const result = moment(date).isSame(moment(), "day")

La solución más simple para ayer:

 const result = moment(date).isSame(moment().subtract(1, 'day'), "day")
over 4 years ago · Santiago Trujillo 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!