/    Sign up×
Community /Pin to ProfileBookmark

Time Script + getHours + getMinutes

Hey there- I have a very simple time script that declares “AM/PM” and “OPEN/CLOSED” depending on the time.

Concerning the “OPEN/CLOSE”, I’m interested in an “OPEN” range between 9:30AM and 4:00PM, and all other times “CLOSED”. I’ve successfully written the script to declare “CLOSED” on the weekends, but need help with the time range. Any help is appreciated! Thanks!

[B]time.js:[/B]

[CODE]
function startclock(){
var thetime=new Date();
var dayNames = new Array(“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”);
var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var OorC=” “;
var AorP=” “;

//Currently “OPEN” between 9AM-12AM : NEED range from 9:30AM-4:00PM
if (nhours>=9)
OorC=”Open”;
else
OorC=”Closed”;

//closed sat and sun
if (dayNames[thetime.getDay()] == “Sunday”)
OorC=”Closed on Sunday”;
else if (dayNames[thetime.getDay()] == “Saturday”)
OorC=”Closed on Saturday”;
else
OorC=”Open”;

if(nhours>=12)
AorP=”PM”;
else
AorP=”AM”;

if (nhours>=13)
nhours-=12;

if (nhours==0)
nhours=12;

if (nsecn<10)
nsecn=”0″+nsecn;

if (nmins<10)
nmins=”0″+nmins;

document.clockform.clockspot.value=” “+nhours+”:”+nmins+”:”+nsecn+” “+AorP+” | “+OorC;
setTimeout(‘startclock()’,1000);
}
[/CODE]

[B]html:[/B]

[CODE]
<form name=”clockform” style=”padding:0px; margin:0px;”>NYSE TIME |<INPUT TYPE=”text” name=”clockspot” readonly=”readonly” size=”26″ style=”padding:0px; margin:0px;” align=”left”></form>
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 14.2008 — function startclock(){
var thetime=new Date();
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var OorC=" ";
var AorP=" ";
if (nhours&gt;=9 &amp;&amp; nhours&lt;=16) {
if (nhours == 9 &amp;&amp; nmins &lt; 30) {
OorC = "Closed";
}
else {
OorC="Open";
}
}
else {
OorC="Closed";
}
//closed sat and sun
if (dayNames[thetime.getDay()] == "Sunday") {
OorC="Closed on Sunday";
}
else if (dayNames[thetime.getDay()] == "Saturday") {
OorC="Closed on Saturday";
}
if(nhours&gt;=12) {
AorP="PM";
}
else {
AorP="AM";
}

if (nhours&gt;=13) {
nhours-=12;
}

if (nhours==0) {
nhours=12;
}

if (nsecn&lt;10) {
nsecn="0"+nsecn;
}

if (nmins&lt;10) {
nmins="0"+nmins;
}

document.clockform.clockspot.value=" "+nhours+":"+nmins+":"+nsecn+" "+AorP+" | "+OorC;
setTimeout('startclock()',1000);
}

I added all the curly braces ({ and }) because it is easier to read, easier to alter and easier to debug. Also you had a slight problem in the original, you were setting open if it wasn't Saturday or Sunday, and that would always override the time setting, regardless of what time it was.

As well as that, you don't actually need the days array, but I left it in just in case you ever want to put the day into the textbox.
Copy linkTweet thisAlerts:
@worked4usauthorJul 14.2008 — Thanks! All i had to tweak was the 16 to 15 for it to display "Closed" at 4PM. Thank you for the super quick response! Oh and I see about the nested conditional, thanks for correcting that. I knew something was wrong there... I'm just new to js, i mostly code in as. Again thank you!

One more question, if you have time to help. I needed to display EST anywhere on the planet. So I found this awesome and robust time script creator: maxxblade

Anyway, it's working as should, but I really would like to display the "OPEN/CLOSED" when appropriate, "OPEN" 9AM-4:30PM M-F, "CLOSED" all else. Just like the script above dictates.

Any additional help would be greatly appreciated! Thanks!

[B]clock.js:[/B]
[CODE]
function tS(){
x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()+dS()-18000000); return x;
}

function tN(){
return new Date();
}

function dS(){
return ((tN().getTime()>fD(0,2,3,2).getTime())&&(tN().getTime()<fD(0,10,3,1).getTime()))?3600000:0;
}

function fD(d,m,h,p){
var week=(p<0)?7*(p+1):7*(p-1),nm=(p<0)?m+1:m,x=new Date(tN().getUTCFullYear(),nm,1,h,0,0),dOff=0; if(p<0){ x.setTime(x.getTime()-86400000);
}
if(x.getDay()!=d){
dOff=(x.getDay()<d)?(d-x.getDay()):0-(x.getDay()-d);
if(p<0&&dOff>0){
week-=7;
}
if(p>0&&dOff<0){
week+=7;
}
x.setTime(x.getTime()+((dOff+week)*86400000));
}
return x;
}

function lZ(x){
return (x>9)?x:'0'+x;
}

function tH(x){
if(x==0){
x=12;
}
return (x>12)?x-=12:x;
}

function dT(){
document.getElementById('tP').innerHTML=eval(oT); setTimeout('dT()',1000);
}

function aP(x){
return (x>11)?' PM':' AM';
}

function oC(x){
return(x>9)?' | Closed':' | Open';
}
var oT="tH(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())+aP(tS().getHours())+oC(tS().getHours())";
if(!document.all){
window.onload=dT;
}else{
dT();
}
[/CODE]

[B]html:[/B]
[CODE]
<span id="tP">&nbsp;</span>
[/CODE]
Copy linkTweet thisAlerts:
@worked4usauthorJul 14.2008 — Thanks! All i had to tweak was the 16 to 15 for it to display "Closed" at 4PM. Thank you for the super quick response! Oh and I see about the nested conditional, thanks for correcting that. I knew something was wrong there... I'm just new to js, i mostly code in as. Again thank you!

One more question, if you have time to help. I needed to display EST anywhere on the planet. So I found this awesome and robust time script creator on: maxxblade

Anyway, it's working as should, but I really would like to display the "OPEN/CLOSED" when appropriate, "OPEN" 9AM-4:30PM M-F, "CLOSED" all else. Just like the script above dictates.

Any additional help would be greatly appreciated! Thanks!

[B]clock.js:[/B]
[CODE]
function tS(){
x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()+dS()-18000000); return x;
}

function tN(){
return new Date();
}

function dS(){
return ((tN().getTime()>fD(0,2,3,2).getTime())&&(tN().getTime()<fD(0,10,3,1).getTime()))?3600000:0;
}

function fD(d,m,h,p){
var week=(p<0)?7*(p+1):7*(p-1),nm=(p<0)?m+1:m,x=new Date(tN().getUTCFullYear(),nm,1,h,0,0),dOff=0; if(p<0){ x.setTime(x.getTime()-86400000);
}
if(x.getDay()!=d){
dOff=(x.getDay()<d)?(d-x.getDay()):0-(x.getDay()-d);
if(p<0&&dOff>0){
week-=7;
}
if(p>0&&dOff<0){
week+=7;
}
x.setTime(x.getTime()+((dOff+week)*86400000));
}
return x;
}

function lZ(x){
return (x>9)?x:'0'+x;
}

function tH(x){
if(x==0){
x=12;
}
return (x>12)?x-=12:x;
}

function dT(){
document.getElementById('tP').innerHTML=eval(oT); setTimeout('dT()',1000);
}

function aP(x){
return (x>11)?' PM':' AM';
}

function oC(x){
return(x>9)?' | Closed':' | Open';
}
var oT="tH(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())+aP(tS().getHours())+oC(tS().getHours())";
if(!document.all){
window.onload=dT;
}else{
dT();
}
[/CODE]

[B]html:[/B]
[CODE]
<span id="tP">&nbsp;</span>
[/CODE]
×

Success!

Help @worked4us spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...