Background
I need to get the current time in 15 minutes brackets.
Code
let bDebug = 0; let arrTimeMinute = [0, 15, 30, 45, 60]; let arrTimeHour = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]; //Declare array var arrTimeBlock = []; function getTimeBlock(arrTime, iCurrent, arrTimeBlock) { // set variables value var iPrevious = 0; var iNext = 0; //iterate through time array for (const arrayContextValue of arrTime) { //when array context value is greater than the current minute //please exit loop if (arrayContextValue > iCurrent) { //save next value iNext = arrayContextValue; break; }; // save current array context as previous value iPrevious = arrayContextValue; }; //remove previously saved entries arrTimeBlock.length = 0; //save values arrTimeBlock.push(iPrevious); arrTimeBlock.push(iNext); }; //get current date and time var objTS = new Date(); //get current time ( as string ) var strTime = objTS.getHours() + ":" + objTS.getMinutes() + ":" + objTS.getSeconds(); //get current minute var iMinuteCurrent = objTS.getMinutes(); //get current hour var iHourCurrent = objTS.getHours(); //display current time ( as string ) console.log("The current time is " + strTime); //call time function getTimeBlock(arrTimeMinute, iMinuteCurrent, arrTimeBlock); //save function result timeBlockMinuteBegin = arrTimeBlock[0]; timeBlockMinuteEnd = arrTimeBlock[1]; //call time function getTimeBlock(arrTimeHour, iHourCurrent, arrTimeBlock); //save function result timeBlockHourBegin = arrTimeBlock[0]; timeBlockHourEnd = arrTimeBlock[1]; if (bDebug == 1) { console.log(""); console.log(""); } // Display previous and next time console.log("Previous Minute is " + timeBlockMinuteBegin); console.log("Next Minute is " + timeBlockMinuteEnd); // Display previous and next hour console.log("Previous Hour is " + timeBlockHourBegin); console.log("Next Hour is " + timeBlockHourEnd);
Output
jsfiddle.net
Tools
Online JavaScript Simulation Environment
Upon Googling, discovered jsfiddle.net ( https://jsfiddle.net/ ).
Went with it.
Works a charm.
Code Sharing
Git / Gist
https://gist.github.com/DanielAdeniji/77cfd4f40ed17a3fcaedd847f6cddc1a