cancel
Showing results for 
Search instead for 
Did you mean: 

Create a date range by hour

Coyote
New Contributor

We asked Snapgpt to create an expression that takes as input a timestamp (start), then the expression will generate the current timestamp (end), then create a range incremented by 1 hour until end is reached ... then the result should be a document of ranges we can feed to a snap:

What Snapgpt generated:

{
   createHourlyIntervals: (start) => {
   var intervals = [];
   var current = Date.parse(start);
   var endTime = Date.now();

   while (current.getTime() < endTime.getTime()) {
      var intervalEnd = current.plusHours(1);

      if (intervalEnd.getTime() > endTime.getTime()) {
          intervalEnd = endTime;
      }

     intervals.push({
        "start": current.toLocaleDateTimeString({"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"}),
        "end": intervalEnd.toLocaleDateTimeString({"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"})
     });

     current = intervalEnd;
     }

     return intervals;
   }
}

Result should look something like this:
[

   {
      "start": "2025-04-30T09:40:23.000Z",
      "end": "2025-04-30T10:40:23.000Z"
   },
   {
      "start": "2025-04-30T10:40:23.000Z",
      "end": "2025-04-30T11:40:23.000Z"
   },
   {   
       "start": "2025-04-30T11:40:23.000Z",
      "end": "2025-04-30T12:40:23.000Z"
   },
...
   {
      "start": "2025-05-01T03:40:23.000Z",
      "end": "2025-05-01T04:40:23.000Z"
   }
]

But it seems Snapgpt has failed us -- it has all sorts of issues. What is wrong with this expression?

1 ACCEPTED SOLUTION

koryknick
Employee
Employee

@Coyote - Sorry that our SnapGPT currently isn't working well for this case.  I have submitted an internal message to our SnapGPT support group and they will look into it.  

In the meantime, I've created a sample pipeline to do what you're looking for.  It's basically just two snaps: a Mapper to generate an array with the start/end timeframes, then a JSON Splitter to convert the array of objects into individual JSON documents.

Please download the attached ZIP file, decompress it, then import the SLP to your Designer.

Let me take just a moment to explain the expression in the Mapper as it contains a few pieces you may not be familiar with:

sl.range(0, Math.ceil(($end - $start) / (1000 * 60 * 60)))
.map(x => 
  { "start" : $start.plusHours(x)
  , "end" : ($start.plusHours(x+1) > $end ? $end : $start.plusHours(x+1))
  } 
)

sl.range is a built-in function that creates an array with incrementing values from start to stop.
Math.ceil is another built-in function that rounds up a float value to the next integer value.
Note that you can perform date-difference logic, with the result as number of milliseconds between.
Next is an Array.map() method that allows you to update the entries in an array.
The syntax that I used in the map method is creating an object with the desired start/end date-time values
The Date.plusHours() method adds the appropriate number of hours generated by sl.range() function
Finally, note the ternary operator syntax used to ensure the final "end" timestamp doesn't go past the current time (given to us in the input document)

Hope this helps!

View solution in original post

2 REPLIES 2

koryknick
Employee
Employee

@Coyote - Sorry that our SnapGPT currently isn't working well for this case.  I have submitted an internal message to our SnapGPT support group and they will look into it.  

In the meantime, I've created a sample pipeline to do what you're looking for.  It's basically just two snaps: a Mapper to generate an array with the start/end timeframes, then a JSON Splitter to convert the array of objects into individual JSON documents.

Please download the attached ZIP file, decompress it, then import the SLP to your Designer.

Let me take just a moment to explain the expression in the Mapper as it contains a few pieces you may not be familiar with:

sl.range(0, Math.ceil(($end - $start) / (1000 * 60 * 60)))
.map(x => 
  { "start" : $start.plusHours(x)
  , "end" : ($start.plusHours(x+1) > $end ? $end : $start.plusHours(x+1))
  } 
)

sl.range is a built-in function that creates an array with incrementing values from start to stop.
Math.ceil is another built-in function that rounds up a float value to the next integer value.
Note that you can perform date-difference logic, with the result as number of milliseconds between.
Next is an Array.map() method that allows you to update the entries in an array.
The syntax that I used in the map method is creating an object with the desired start/end date-time values
The Date.plusHours() method adds the appropriate number of hours generated by sl.range() function
Finally, note the ternary operator syntax used to ensure the final "end" timestamp doesn't go past the current time (given to us in the input document)

Hope this helps!

Thanks Kory - I'm apologize ... my criticism of Snapgpt was bad humor.  I will say that Chatgpt produced a similar solution to Snapgpt, so they must be colluding.  Thank you for this solution - I will study what you produced.