Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return epoch of all midnights in given time span?

I have a time span between two epoch times (in seconds). I want a function that returns the epoch times (in seconds) of all midnights within that time span.

In pseudocode I would want something like this:

const epoch_start = 1600000;
const epoch_end = 16040000;

function getMidnights(start, end){
    // do your magic    
}

console.log(getMidnights(epoch_start, epoch_end));

I would expect the return of that function to look like this: [1600020, 1600400] (these are just example values).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What would be the most efficient way to do this?

My ideas were: get unique list of days within range and return their midnight.

>Solution :

You might step by day rounding by day:

const epoch_start = 1600000000;
const epoch_end   = 1600400000;
const day = 86400;

function getMidnights(start, end){
    midnites = [];
    while (start < end) {
      midnites.push((start/day + .5|0)*day);
      start += day;
    }
    return midnites;
}

console.log(getMidnights(epoch_start, epoch_end))
console.log('* check it *')
console.log(getMidnights(epoch_start, epoch_end).map(t => new Date(t*1e3)));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading