Getting the time typed in javascript

Asked

Viewed 82 times

0

Person, I have a simple form where the user should type 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 to the angular: inserir a descrição da imagem aqui

I need just the hours and minutes (17:17)... How do I get only this data? I’m trying to use the "split()" javascript, but in the console appears a warning saying that split is not a function.

  • https://stackoverflow.com/a/19346876/4551469 help this link

  • 1

    I think so @rLinhares

  • Here without using the split, simpler: var horaMinuto = new Date().toString().match(/\d{2}\:\d{2}/)[0];

  • I don’t understand how Moment.js picks up the date informed by the user... From what I saw in the documentation it picks up the data from the system.

1 answer

0

Split error()

The point here is that split() needs to be used in a string and you’re trying to use it in 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 take 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 to time:

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

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

  • But this way I’m getting the date and time of the system, but I need to get the data that the user informed, understand?

  • Check the update.

  • Ahhhh now I understand. Thanks.

Browser other questions tagged

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