Forum Discussion

mcbane's avatar
mcbane
Contributor
4 years ago

Javascript for loop using date as counter

Hi. I'm trying to create a for loop using a date starting with 1/1/2021 and going to today's date as below:

 

for(var d = aqDateTime.SetDateElements(2021, 01, 01); d <= aqDateTime.Now(); aqDateTime.AddDays(d, 1))
{

...

}

 

The var d is not incrementing by 1 day each time. It's staying constant at 01/01/2021. Can someone point me in the right direction? I had it working when I was using a javascript date object, but that creating other hurdles inside the for loop. I really want to use a DateTime (or Date) in TestComplete's native date format. Thanks!

  • Hi,

     

     It's staying constant at 01/01/2021.

    It stays constant because you do not change it.

    Try this:

    for(var d = aqDateTime.SetDateElements(2021, 01, 01); d <= aqDateTime.Now(); d = aqDateTime.AddDays(d, 1))

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

     It's staying constant at 01/01/2021.

    It stays constant because you do not change it.

    Try this:

    for(var d = aqDateTime.SetDateElements(2021, 01, 01); d <= aqDateTime.Now(); d = aqDateTime.AddDays(d, 1))

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thank you for sharing this idea, Alex!

       

      mcbane does this help?

      • mcbane's avatar
        mcbane
        Contributor

        Thank you. I really do appreciate the response. I had actually bulled my way through the javascript object method. I did plug this in real quick to my aqConvert version of the function, and yes, it works.

         

        Thank you so much.

         

        Tommy