Jump to content

Write a Javascript code to display text on a webpage based on time and day of week.


Recommended Posts

Is there anyone that write me a javascript code that will display a certain message based on what time of day it is and what day of the week it is. I run a radio stations website and they want to put up a schedule of their on-air staff and shows on their website.Here is a code i found that worked, but it's just time in general, not what day of the week. Also, how can I make it to get the time in a certain time zone?

No message at the present timefunction show() {var nowhrs = new Date().getHours();var nowmins = new Date().getMinutes();var hrsmins = nowhrs + (nowmins/60);  // decimals of an hourvar schedule = [['Message 1',[9],[18]],  // times are expressed as hours + decimals of an hour, e.g. 30 minutes is .5 hours.['Message 2',[18],[21.5]],['Message 3',[19.5],[20.25]]]var themessage = "";for (var i =0; i = schedule[i][1]) && (hrsmins < schedule[i][2])) {themessage += schedule[i][0]  + "";document.getElementById("mymessage").innerHTML = themessage;}}}
Link to post
Share on other sites

Here is the code:<html><head></head><body onload = "show()"><div id = "mymessage">No message at the present time</div><script type = "text/javascript">function show() {var nowhrs = new Date().getHours();var nowmins = new Date().getMinutes();var hrsmins = nowhrs + (nowmins/60); // decimals of an hourvar schedule = [['Message 1',[9],[18]], // times are expressed as hours + decimals of an hour, e.g. 30 minutes is .5 hours.['Message 2',[18],[21.5]],['Message 3',[19.5],[20.25]]]var themessage = "";for (var i =0; i <schedule.length; i++) {if ((hrsmins >= schedule[1]) && (hrsmins < schedule[2])) {themessage += schedule[0] + "<br>";document.getElementById("mymessage").innerHTML = themessage;}}}</script></body></html>

Link to post
Share on other sites

Here's a reference for the date object:

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

 

That shows all of the properties and methods you can use. It has a method called getDay, for example, which returns a number between 0 and 6 which indicates which day it is. You can use the getTimezoneOffset method to figure out what the offset is for the timezone, but there's not a way to set the timezone for the date object because the timezone is always set to whatever the user's computer is set to. Since Javascript runs in the browser and not the server, the timezone will be set to whatever the user's computer is set to. If you always want to show dates in the same timezone then you'll either need to get the user's timezone offset and use that to convert the time to whatever timezone you want to show, or you'll need to manage the time display on the server instead of in the browser. Note that the user's clock will also be used in Javascript, if they have their date set to 1/1/2000 then that's what Javascript is going to show.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...