1

I'm trying to get a list of hours between two date

I've tried do an while no get them, but I dont know why my app is freezing when I call the function

 getHours(){

    let dataA =new Date();
    let dataF = new  Date();
    dataA.setHours(this.state.lojas.data[0].h_abertura.split(':')[0],this.state.lojas.data[0].h_abertura.split(':')[1]);
    dataF.setHours(this.state.lojas.data[0].h_fecho.split(':')[0],this.state.lojas.data[0].h_fecho.split(':')[1]);

    var listaDatas = [];

    console.log(dataF.toString())




    while(Math.abs((( dataA.getTime() - dataF.getTime()) / 60000)) > parseInt(this.state.lojas.data[0].tempoAtender))
    {

       listaDatas.push(dataA.getHours()+":"+dataA.getMinutes());
        dataA.setMinutes(dataA.getMinutes()+ this.state.lojas.data[0].tempoAtender);





    }



 }

Math.abs((( dataA.getTime() - dataF.getTime()) with this i'm getting 540 as result and if I add minutes "dataA.setMinutes(dataA.getMinutes()+ this.state.lojas.data[0].tempoAtender);" and check it again i got 520 but it seens like that it is entering on a infinity loop

Resolution


    let dataA =new Date();
    let dataF = new  Date();
    dataA.setHours(this.state.lojas.data[0].h_abertura.split(':')[0],this.state.lojas.data[0].h_abertura.split(':')[1]);
    dataF.setHours(this.state.lojas.data[0].h_fecho.split(':')[0],this.state.lojas.data[0].h_fecho.split(':')[1]);

    var a = moment(`2016-06-06T${this.state.lojas.data[0].h_fecho.split(':')[0]}:${this.state.lojas.data[0].h_fecho.split(':')[1]}:55`);//now
    var b = moment(`2016-06-06T${this.state.lojas.data[0].h_abertura.split(':')[0]}:${this.state.lojas.data[0].h_abertura.split(':')[1]}:55`);

    var listaDatas = [];

    console.log(dataF.toString())




    while(a.diff(b,'minutes') >= parseInt(this.state.lojas.data[0].tempoAtender))
    {

       listaDatas.push(b.hour()+":"+b.minutes())
        b.add(this.state.lojas.data[0].tempoAtender,'minutes')





    }

    listaDatas.push((b.hour()+":"+b.minutes()).toString())
    alert(listaDatas.toString())

  • 2
    If you can use [Moment](http://momentjs.com/docs/#/durations/hours/), check this out https://stackoverflow.com/questions/25150570/get-hours-difference-between-two-dates-in-moment-js Frankly, I usually don't do date manipulation manually, my feeble brain is too often confused and error-prone, and libraries like Moment just make problems like this a two-liner. – msanford Jun 19 '19 at 20:33
  • 1
    I think the problem is on the while.When I just duplicate the code a lot of times it works, cause of that I got little confused.But I will try use Moment,ty – Andrew Mautone Jun 19 '19 at 20:37

1 Answers1

1

I believe your problem is where you are incrementing dataA:

    dataA.setMinutes(dataA.getMinutes()+ this.state.lojas.data[0].tempoAtender);

dataA.getTime passed your while condition the first time. It seems like you are just making it bigger, not smaller, unless this.state.lojas.data[0].tempoAtender is negative

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • my dataF is bigger than the A , when I add this minutes, It make the time between the two dates smaller. I've did it manually repeating the code a lot of time and it is working , the time between A and F is smaller every time it run – Andrew Mautone Jun 19 '19 at 20:45