Date Today Moments.js

Asked

Viewed 808 times

-1

I’m using Moment.js with the following code, and in it is to generate the due date all month according to the selected date, but when I select today’s date, it appears written Today at 12:00 am and not 28/04/2018, how to correct this error?

for ($i = 0; $i < 12; $i++){
var minhaData = moment(ParcelaVencimento, "YYYY/M/D h:m").add('months', $i);

    ParcelaVencimento1 =  minhaData.calendar();

    var linha = '<tr class="selected" id="linha'+cont+'">    <td> <button type="button" class="btn btn-warning" onclick="apagar('+cont+');"> X </button></td>      <td> <input type="hidden" name="cont[]" value="'+cont+'">'+cont+'</td>   <td> <input type="text" name="ParcelaVencimento1[]" value="'+ParcelaVencimento1+'"></td>  </tr>'

Date

Today at 12:00 am

28/05/2018

28/06/2018

28/07/2018

1 answer

0

Correct is to use the method format() instead of calendar(). Another point to note is that the method add() is receiving format parameters that has been discontinued, instead of add('months', $i) use add($i, 'months')

Working example

var ParcelaVencimento = '2018-04-01';
for ($i = 0; $i < 12; $i++) {
  var minhaData = moment(ParcelaVencimento).add($i, 'months');
  console.log(minhaData.format('DD/MM/YYYY'))
}
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.