Jump to content

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


ramig

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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...