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

337
Views
Formatee la fecha y reste días usando Moment.js

Me gustaría una variable para contener la fecha de ayer en el formato DD-MM-YYYY usando Moment.js. Entonces, si hoy es 15-04-2015, me gustaría restar un día y tener 14-4-2015.

He probado algunas combinaciones como esta:

 startdate = moment().format('DD-MM-YYYY'); startdate.subtract(1, 'd');

y esto:

 startdate = moment().format('DD-MM-YYYY').subtract(1, 'd');

y tambien esto:

 startdate = moment(); startdate.subtract(1, 'd'); startdate.format('DD-MM-YYYY')

Pero no lo estoy entendiendo...

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Tienes múltiples rarezas sucediendo. El primero ha sido editado en su publicación, pero tenía que ver con el orden en que se llamaban a los métodos.

.format devuelve una cadena. String no tiene un método de subtract .

El segundo problema es que está restando el día, pero en realidad no lo está guardando como una variable.

Su código, entonces, debería verse así:

 var startdate = moment(); startdate = startdate.subtract(1, "days"); startdate = startdate.format("DD-MM-YYYY");

Sin embargo, puede encadenar esto juntos; esto se vería como:

 var startdate = moment().subtract(1, "days").format("DD-MM-YYYY");

La diferencia es que estamos configurando startdate a los cambios que estás haciendo en startdate, porque el momento es destructivo.

over 4 years ago · Santiago Trujillo Report

0

startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');
over 4 years ago · Santiago Trujillo Report

0

var date = new Date(); var targetDate = moment(date).subtract(1, 'day').toDate(); // date object

Ahora, puede formatear cómo quiere ver esta fecha o puede comparar esta fecha con otra, etc.

la función toDate() es el punto.

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!