So, I was asked to write a function today by another Pujie Black user… they wanted to have a way to calculate 24 hour digits back and forward from the current hour… I wrote this function…

function hrOffset (hr,offset,maxhr) {
hr = (hr+offset)%maxhr;
hr=hr<0&&maxhr==24?hr+maxhr:hr;
hr=hr<=0&&maxhr==12?hr+maxhr:hr;
return ('0'+hr).slice(-2);
}

I then provided these usage examples:

//for 12hr plus 2
return hrOffset ([h12],2,12)
//for 12hr plus 1
return hrOffset ([h12],1,12)
//for 12hr minus 1
return hrOffset ([h12],-1,12)
//for 12hr minus 2
return hrOffset ([h12],-2,12)

//for 24hr plus 2
return hrOffset ([h24],2,24)
//for 24hr plus 1
return hrOffset ([h24],1,24)
//for 24hr minus 1
return hrOffset ([h24],-1,24)
//for 24hr minus 2
return hrOffset ([h24],-2,24)

It is worth noting that I personally use a similar method when constructing my Minimal & Elegant replica…

For negative hours…

function hrOffset (hr,offset,maxhr) {
hr = (hr+offset)%maxhr;
hr=hr<0&&maxhr==24?hr+maxhr:hr;
hr=hr<=0&&maxhr==12?hr+maxhr:hr;
return ('0'+hr).slice(-2);
}
var txt="";
for (var i=-1;i>-4;i--) {
txt = hrOffset([h24],i,24)+" "+txt;
}
return txt

For positive hours…

function hrOffset (hr,offset,maxhr) {
hr = (hr+offset)%maxhr;
hr=hr<0&&maxhr==24?hr+maxhr:hr;
hr=hr<=0&&maxhr==12?hr+maxhr:hr;
return ('0'+hr).slice(-2);
}
var txt="";
for (var i=1;i<4;i++) {
txt = txt+" "+hrOffset([h24],i,24);
}
return txt

For concatenated negative minutes…

function minOffset (min,offset) {
min = (min+offset)%60;
min=min<0?min+60:min;
return ('0'+min).slice(-2);
}
var txt="";
for (var i=-1;i>-5;i--) {
txt = minOffset([m],i)+" "+txt;
}
return txt

For concatenated positive minutes…

function minOffset (min,offset) {
min = (min+offset)%60;
min=min<0?min+60:min;
return ('0'+min).slice(-2);
}
var txt="";
for (var i=1;i<5;i++) {
txt = txt+" "+minOffset([m],i);
}
return txt

Leave a Reply

Your email address will not be published. Required fields are marked *