Found a script that works well if you take a period of time, for example, from 13:00 to 18:00, but if I want to take a period, say from 22:00 to 02:00, then the interval is not displayed correctly, how can I solve this problem? The essence of the script: takes a time start_time and end_time in timestamp format and splits into a period of 30 minutes

function times(start_time, end_time) { var data = [ [start_time, end_time] ], times = [], pair, i, n30, D = new Date(), H, M; for( pair=0; pair < data.length; pair++){ n30 = (data[pair][1] - data[pair][0]) / 1800 ; for( i=0; i<=n30; i++){ D.setTime( 1000 * (data[pair][0] + i*1800)); H = D.getUTCHours() ; M = D.getUTCMinutes(); times.push( ('<p>') +( H<10 ? '0'+H : H) +':' +( M<10 ? '0'+M : M) +('</p>') ); } } } 

I am conveying time from 2016-11-14 01:00:00 UTC at 946767600 (timestamp)

  • look towards the moment.js - Vyacheslav Danshin
  • you have setTime used there. It probably leaves part of the date unchanged and only changes time. Therefore, the transition to the new day gives not what we need. - teran
  • Take the initial date-time, translate into the timestamp. further in the cycle, add 1800 seconds each, but print the resulting time. - teran 2:21
  • @teran thanks for the comments, they helped to understand what the problem was - Silentium

1 answer 1

In general, I added that if the starting time is more than the end, then we add the day to the end:

 if (start_time > end_time){ end_time = end_time + 86400; }