Realmente me quedé atrapado en esta fecha. Digamos que hoy es 01 20 2021, y quiero agregar 14 días después para obtener la fecha de vencimiento. entonces, la fecha de vencimiento debería ser el 02 03 2021, pero solo obtengo el 01 03 2021. ¿Alguien puede ayudarme con esto? Gracias
este es mi codigo
var today = new Date(); var monthIndex = today.getMonth()+1; var year = today.getFullYear(); var addday = new Date(new Date('1.20.2021').getTime()+(14*24*60*60*1000)); var nextDay = addday.getDate(); var nextMonth = monthIndex; console.log('day',day) return nextMonth+'.'+nextDay+'.'+year //the return that I want is 2.03.2021No actualizó el valor de monthIndex. Debes usar:
var nextMonth = addday.getMonth()+1;Simplemente use la función Date.setDate para esto.
// new Date('1.20.2021') => Jan 20 2021 const dateStr = '1.20.2021'; const [mm, dd, yyyy] = dateStr.split('.'); const today = new Date(yyyy, +mm-1, dd); // add 14 days const newDate = new Date(new Date(today).setDate(today.getDate() + 14)); console.log(`Today: ${today}`); console.log(`New Date: ${newDate}`); console.log(newDate.getMonth() + 1 + '.' + newDate.getDay() + '.' + newDate.getFullYear());