How to use moment.js with date coming from an input date?

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 do not want it to come out in this format

  

DateTitle 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 entered in the fields and using moment.js

    
asked by anonymous 03.04.2018 / 21:07

1 answer

3

I do not know if I understood your question, but assuming your input returned something like 2018-04-03T03: 00: 00ZZ )

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

You can use Momen t 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, depending on the form you want to save.

    
03.04.2018 / 21:29