Home>

How to split a date/time into an interval, i.e. there is a start time and an end time

let start= 1595902440886; //start time
let end= 1595904296582; //end time

It is necessary, depending on the range, to split into time, the output should be in the form of an array

Example:

[ 15:00, 15:30, 14:00, 14:30, 15:00 ] //for minutes
[ 1.04.2020, 2.04.2020, 3.04.2020, 4.04.2020, 5.04.2020 ] //for days
//... and so on, different options depending on the distance start and end

This is the interval between dates or times, depending on the length, to display after this time on the chart

Examples of intervals are 2 minutes, 5, 10, 30, 1 hour, etc.

Sergey Mironov2022-02-14 12:00:48

what if the start is at 3.17, but you need to mark every half an hour?

MBo2022-02-14 12:00:48

@MBo, and here I think it doesn’t matter what at the beginning, I’m trying to do it like in regular charts, like on tradingview.com, for example, the wider the range, the rarer the date

Sergey Mironov2022-02-14 12:00:48

No, the question is whether to put the first mark at 3.00, 3.17 or 3.30. And why does such a partitioning need to be done manually if there are libraries for constructing beautiful graphs?

MBo2022-02-14 12:00:48

@MBo, it will be possible to display only 3.30 on the chart, it turns out the beginning in the 3.30 array, as I understand it

Sergey Mironov2022-02-14 12:00:48
  • Answer # 1

    Splitting into labels is elementary:

    function splitInterval(start, end, step) {
      let result= [];
      for (let ts= start; ts < end; ts += step) {
        result.push(ts);
      }
      if (result.length== 1) {
        result.push(end);
      }
      return result;
    }
    //****************************************
    let start= 1595902440886; //start time
    let end= 1595904296582; //end time
    let step= 2 * 60 * 1000; //step in milliseconds (2 minutes)
    let result= splitInterval(start, end, step);
    console.log('Start: ' + (new Date(start)).toLocaleString());
    console.log('End: ' + (new Date(end)).toLocaleString());
    for (let i= 0; i < result.length; ++i) {
      console.log('Step ' + i + ': ' + (new Date(result[i])).toLocaleString());
    }

    But there are some remarks about your TOR:

    1. The first label must start exactly from the start time. Trying to round it will lead to numerous logical problems. Think that if you have a start time of 3:14 and a stride length of say 17 seconds. Then where should the first label be? That is why the first label must match the start time.

    2. Your main task is to break any interval into labels. The question in what format to display these labels on the screen is individual for each partition. Trying to do something universal will lead to logical errors. Think about what if you have an interval of, say, more than 1-2 days and you divide it into steps of 1 hour in length, and output it as[01:00:00], [02:00:00], [03:00:00] .... Do not forget also that months have a variable number of days, and the output of beautiful dates like[01.01.2000], [02.01.2000], [03.01.2000] ...implies a variable length of steps. Therefore, the final label format must be decided individually for each partition.

    In general, normal libraries for working with charts are arranged in such a way that only raw data is transferred to them. The graph is built and drawn based on raw data. But the labels are formatted at the time of their rendering, according to the format that the user sets individually.

  • Answer # 2

    Splitting into labels is elementary:

    function splitInterval(start, end, step) {
      let result= [];
      for (let ts= start; ts < end; ts += step) {
        result.push(ts);
      }
      if (result.length== 1) {
        result.push(end);
      }
      return result;
    }
    //****************************************
    let start= 1595902440886; //start time
    let end= 1595904296582; //end time
    let step= 2 * 60 * 1000; //step in milliseconds (2 minutes)
    let result= splitInterval(start, end, step);
    console.log('Start: ' + (new Date(start)).toLocaleString());
    console.log('End: ' + (new Date(end)).toLocaleString());
    for (let i= 0; i < result.length; ++i) {
      console.log('Step ' + i + ': ' + (new Date(result[i])).toLocaleString());
    }

    But there are some remarks about your TOR:

    1. The first label must start exactly from the start time. Trying to round it will lead to numerous logical problems. Think that if you have a start time of 3:14 and a stride length of say 17 seconds. Then where should the first label be? That is why the first label must match the start time.

    2. Your main task is to break any interval into labels. The question in what format to display these labels on the screen is individual for each partition. Trying to do something universal will lead to logical errors. Think about what if you have an interval of, say, more than 1-2 days and you divide it into steps of 1 hour in length, and output it as[01:00:00], [02:00:00], [03:00:00] .... Do not forget also that months have a variable number of days, and the output of beautiful dates like[01.01.2000], [02.01.2000], [03.01.2000] ...implies a variable length of steps. Therefore, the final label format must be decided individually for each partition.

    In general, normal libraries for working with charts are arranged in such a way that only raw data is transferred to them. The graph is built and drawn based on raw data. But the labels are formatted at the time of their rendering, according to the format that the user sets individually.