Search code examples
javascriptunix-timestamp

how I can get hours range from timestamp?


I have two timestamps from and To of working hours.

In this case From is: [today] 09:00 (24h) - To: [today] 18:00 (24h).

I would like to get the hours from 0:00 to From-hour and from To to last day hour.

I need to know the "non-working" hours to disable these hours in my calendar.

So I can disable in my calendar like this:

Disable: 00:00 to 09:00

Disable: 18:00 to 23:59

How can I get the hours from the beginning of the day to the first hour of From and from the To hour to the end of the day?

Is this doable with javascript?

var from = 1633935600;
var to = 1633968000;

var f_1 = new Date(from * 1000).getHours();
var t_1 = new Date(to * 1000).getHours();

console.log(f_1);
console.log(t_1);

Thanks.


Solution

  • You can get the outside hour length by subtracting f_1 from t_1:

    var from = 1633935600;
    var to = 1633968000;
    
    var f_1 = new Date(from * 1000).getHours();
    var t_1 = new Date(to * 1000).getHours();
    
    let outsideHourDuration = t_1 - f_1;
    
    console.log('outside hour start:', f_1)
    console.log('outside hour end:', f_1 + outsideHourDuration)