Include hour with minute in a javascript hour array

Asked

Viewed 186 times

-1

I have a method that generates an array of number time and string time, one to show in the table and one to send in the json. The point is I’d like to include 01:00 / 01:30 - 02:00 so on.

function listaHorasDia() {
    var horas= [];
    for (var h = scope.horaInicio; h < (scope.horaFinal +1); h++)
        horas.push({ horaNumero: h, horaExibicao: ('00' + h).substr(-2) });
    return horas;
}

I would like my array to look like this

horas = [
{horaNumero:1330, horaExibicao: '01:30'},
{horaNumero:1400, horaExibicao: '14:00'}
] 

and so on

  • Could detail better what you want to include?

  • edited the question @Felipeavelar

  • But do you already get this at Scope? Or do you want the increment to be every 30 minutes?

  • on Scope I get an entire number, I would like inside the for be done that @Felipeavelar

  • Wouldn’t it be the case to just add one more push no? In case you would add this push: horas.push({ horaNumero: h+30, horaExibicao: h.toString().substr(-2)+':30' });?

  • In that case I lose the whole hour 0: {horaNumero: 30, horaExibicao: "0:30"}&#xA;1: {horaNumero: 31, horaExibicao: "1:30"}&#xA;2: {horaNumero: 32, horaExibicao: "2:30"}&#xA;3: {horaNumero: 33, horaExibicao: "3:30"}

Show 1 more comment

1 answer

0

From what was passed in the comments, it probably solves your problem:

function listaHorasDia() {
    var horas= [];
    for (var h = scope.horaInicio; h < (scope.horaFinal +1); h++) {
        horas.push({ horaNumero: h, horaExibicao: ('00' + h).substr(-2) });
        horas.push({ horaNumero: h+30, horaExibicao: h.toString().substr(-2) + ':30' });
    }
    return horas;
}

Browser other questions tagged

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