How to use Moment.js with date from an input date?

Asked

Viewed 1,603 times

0

I have the following form:

<form name="formConsultaPeriodo">
    Data Inicial
    <input class="form-control dataIni" type="date" name="dataIni" ng-model="consulta.dataInicial" required />
    Data Final
    <input class="form-control dataFim" type="date" name="dataFim" ng-model="consulta.dataFinal" required />
    <a class="btn btn-success" ng-disabled="formConsultaPeriodo.$invalid" ng-click="consultaPeriodo(consulta)">Consultar</a>
</form>

My controller:

$scope.consultaPeriodo = function(consulta){
    var dataInicial = consulta.dataInicial;
    var dataFinal = consulta.dataFinal;
    console.log("dataInicial "+dataInicial+" dataFinal "+dataFinal);

}

I don’t want it to come out in this shape

dataInitil Thu Jan 01 2015 00:00:00 GMT-0200 (-02) dataFinal Fri Jan 01 2016 00:00:00 GMT-0200 (-02)

I want to know how to format the dates typed in the fields and using Moment.js

1 answer

3


I’m not sure I understand your question, but supposing your input returned something like (2018-04-03T03:00:00.000Z)

const consulta.dataInicial = "2018-04-03T03:00:00.000Z";

you can use the Moment as follows:

moment(consulta.dataInicial).format('YYYY-MM-DD HH:mm:ss');
// 2018-04-03 00:00:00

or

moment(consulta.dataInicial).format('DD-MM-YYYY HH:mm:ss');
// 03-04-2018 00:00:00

or

moment(consulta.dataInicial).format('DD/MM/YYYY');
// 03/04/2018

There are several ways, it depends on the way you want to save.

  • So I would have to take the variable with the date and put it into Moment? That way? Moment("startData"). format('YYYY-MM-DD HH:mm:ss');

  • That’s right, thanks Glauber

Browser other questions tagged

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