Getting the time typed in javascript

0

Person, I have a simple form where the user should enter the time.

<form name="formAgendamento" class="formAgendamento">
    <label>Dia</label>
    <input class="form-control dia" type="date" name="dia" ng-model="agendamento.dia" required>
    <label>Hora</label>
    <input class="form-control hora" type="time" name="hora" ng-model="agendamento.hora" required>
    <button class="btn btn-primary" ng-disabled="formAgendamento.$invalid" ng-click="agendar(agendamento)">Agendar</button>
</form>

But the time goes in the following format for the angle:

Ineedonlyhoursandminutes(17:17)...HowdoIgetjustthisdata?I'mtryingtousethe"split ()" of the javascript, but in the console a warning appears saying that split is not a function.

    
asked by anonymous 10.08.2017 / 22:51

1 answer

0

Error in split ()

The question here is that split () needs to be used in a string and you are trying to use it on an object.

    var hora = new Date(); // Thu Aug 10 2017 21:19:18 GMT-0300 (BRT)
    typeof data;           // object

If you do

    data.split(" ");       // Terá o erro 'data.split is not a function'

Solving

Then you need to get the date as a string and then use the split. It can be done like this:

    var data = new Date().toString();
    typeof data;          // Você verá "string"
    var dataSeparada = data.split(" ");
    dataSeparada[4];      // "21:21:55"
    var horaMinuto = dataSeparada[4].substring(0,5)
    horaMinuto;           // "21:21"

Update

In your vector, to catch the day use:

    dia.toString().match(/\d{2}/)[0]

And for the hour:

    hora.toString().match(/\d{2}:\d{2}/)[0]

Regex as done by Leandro Simões will help a lot.

    
11.08.2017 / 02:30