How can I separate working hours with javascript?

62
December 25, 2017, at 5:18 PM

This is my example.

9.00 - 18.00

I want to separate working hours like this

9.00 - 10.00
10.00 - 11.00
11.00 - 12.00
12.00 - 13.00
13.00 - 14.00
14.00 - 15.00
15.00 - 16.00
16.00 - 17.00
17.00 - 18.00

This is my code,it is working well

let facility[0]['working_hours'] = 9.00 - 18.00
//SEPARATED WORKING HOURS
            let dateArray  = facility[0]['working_hours'].match(/\d+/g);
            let time_1 = parseInt(dateArray[0]);
            let time_2 = parseInt(dateArray[2]);
            let start_date_time = [],end_date_time = [];
            let j = 0;
            for(let i = time_1; i < time_2 + 12; i++){
                start_date_time[j] = `${i > 12 ? i - 12 : i}.00`;
                end_date_time[j] = `${i + 1 > 12 ? i + 1 - 12 : i + 1}.00`;
                j++;
            }

But I want to change my code,making it more stable using moment.js or another good example which you suggest.

Answer 1

// Initial input 
const hours = '9.00 - 18.00'; 
 
// '9.00 - 18.00' -> [ 9, 18 ] -> const start = 9, const end = 18 
const [start, end] = hours.split(' - ').map(parseFloat); 
 
// Push each hour block to allHours. 
const allHours = []; 
for (let curStart = start; curStart < end; curStart++) { 
  // Note: need toFixed(2) here to convert from number 9 to string '9.00' 
  allHours.push(`${ curStart.toFixed(2) } - ${ (curStart + 1).toFixed(2) }`); 
} 
 
console.log(allHours);

Answer 2

If I understood it right, you just need to update the last part of your code to:

let working_hours = [[], []];
for(let i = time_1; i < time_2; i++){
    working_hours[0].push(`${i}.00`);
    working_hours[1].push(`${i + 1}.00`);
}
Edit:

Now using moment.js

let working_hours = '9.00 - 18.00';
let dateArray  = working_hours.split(' - ');
let start = moment(dateArray[0], 'H.m');
let end   = moment(dateArray[1], 'H.m');
let start_date_time = [],end_date_time = [];
let max = start.hour() + end.diff(start, 'hours');
for(let hour = start.hour(); hour < max; hour++){
    start_date_time.push(`${hour}.00`);
    end_date_time.push(`${hour + 1}.00`);
}
READ ALSO
SearchView Android- with server side

SearchView Android- with server side

I have nodejs server connected to MongoDb, is it possible to connect it to the search option in android studio? I mean that the results of the searching(in the search view) will be shown from the db which is connected to my node

49
How do I get the Forevermark button at the top of this page to be clickable?

How do I get the Forevermark button at the top of this page to be clickable?

My Forevermark button isn't working hereIt's only clickable when I set the position of the video as relative, but then my video overlaps the button

51
Bug with iOS render of display: flex?

Bug with iOS render of display: flex?

I'm using HTML5 display and summary elementsI'm using a custom marker as a ::before pseudo element

65
Buttons inside Tablepress Plugin

Buttons inside Tablepress Plugin

I would like to add buttons inside the tablepress wordpress plugin box on my domain https://bloggersneedcom while viewing on my browser the buttons are displaying correctly but after i logged out and seeing my website on VPN the buttons are breaking like this:

60