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

114
Views
Obtener una diferencia de mes incorrecta entre 2 fechas

Tengo 2 fechas como a continuación:

 Todays date = 2021-10-13 11:03:57.560 Old date = 2016-08-07 11:03:57.560

Quiero diferencia de mes de todays date Month - Old date Month = 10 - 8 = 2

Código:

 console.log(moment().diff($scope.creationTime, 'months')); // returns 62

Esperado: Mes actual - Mes de fecha anterior

¿Puede alguien por favor ayudarme con esto?

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

0

En este momento obtienes 62 porque las dos fechas tienen 5 años y 2 meses de diferencia:

5 * 12 + 2 = 62

Una manera fácil de descartar la diferencia de año es usar el operador de resto y hacer monthDifference % 12 , lo que le dará solo la cantidad de meses, independientemente de los años:

 function monthDiff(date1, date2) { const monthDifference = moment(date1).diff(moment(date2), 'months'); return monthDifference % 12; } console.log(monthDiff("2021-10-13 11:03:57.560", "2016-08-07 11:03:57.560")); console.log(monthDiff("2021-10-07 11:03:57.560", "2016-08-13 11:03:57.560"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

Use month() de moment.js para obtener los meses y las matemáticas simples después de eso:

 const today = moment(); const someday = moment('2011-01-01 00:00Z'); console.log(today.month()) console.log(someday.month()) console.log(Math.abs(today.month()-someday.month()))
about 4 years ago · Juan Pablo Isaza Report

0

No necesita un momento para hacer eso, también usa Fecha

 new Date().getMonth - to get your current month new Date('2016-08-07 11:03:57.560').getMonth() - to get the month of your old date 

 console.log(new Date().getMonth() - new Date('2016-08-07 11:03:57.560').getMonth())

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!