Click here to Skip to main content
12,819,019 members (28,110 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 17 Feb 2014

Running JavaScript at Specified Times: Timed JavaScript

, 17 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Running JavaScript at specified times: Timed JavaScript


In this article, I will show you how to run a JavaScript code selectively. Sometimes the requirements are like we want to run a specific section of script on a specific day or at a specific time of the day. For example, let’s consider the following situations:

  • If you want to change the background of a website on some special days like Christmas.
  • If you want a different look for your website at a specific time of the day.

In all of the preceding cases, this article will be very useful. So let’s start our discussion by starting with the first example.

The Date Class

The Date object of JavaScript is very important to understand before we code the preceding situations. It is very big and it’s not possible to cover it completely in this article. We will discuss only those methods of this class that we will use in our script.

  • getDate: Returns the day of the month (1-31) for the specified date depending on local time. It doesn’t take any parameter.
  • getDay: Returns the day of the week (0-6) for the specified date depending on local time. Always remember that count of days start from 0 in JavaScript.
  • getHours(): Returns the hour (0-23) in the specified date depending on local time. Always remember that the count of hours starts from 0.
  • getMinutes(): Returns the minutes (0-59) in the specified date depending on local time. Always remember that the count of hours starts from 0.
  • getMonth(): Returns the month (0-11) in the specified date depending on local time. Always remember that the count of hours starts from 0. So for December that is 12th month is represented by 11 in JavaScript.
  • getSeconds(): Returns the seconds (0-59) in the specified date depending on local time.

Running Script on Special Days

Use the following procedure to do it:

  1. First, we will detect the current date.
  2. Second, we will compare the current date with our goal day.
  3. Thirdly, we will execute the script if the result of the second step is true.



<!DOCTYPE html>
<link href=“” rel=“stylesheet”  type=“text/css”/>
<script src=“”></script>
<script src=“”></script>
<meta charset=utf-8/> 
<title>JS Bin</title>
  <div id=“dlg”title=“Congratulations”><p>Happy independence day!</p></div>




1.function test(){
2.   var d=new Date();
3.   if(d.getDate()==15&& d.getMonth()+1 == 8){//15th Aug
4.     // special script
5.      $(“#dlg”).dialog();
6.   }else{
7.     //rest of the script
8.   }
  • Line number 2 creates an instance of date using the date constructor. The Date constructor with zero arguments returns the current date of system.
  • Line number 3 compares the day of today (using getDate) with 15 and today’s month (using getMonth) with 8 (7+1 for Aug). This line checks whether today is 15th August or not.
  • If line 3 evaluates to true, then a happy independence message will be shown else the rest of the script is executed.


Running Script at Specific Time of the Day

Here, we will execute our script at a specific time of the day. Let’s say we want to alert user for “Happy Hours”, we can use it.


<!DOCTYPE html>

<title>JS Bin</title>
  <divid=“dlg”title=“Congratulations”><p>Free !Sale! period is running!</p></div>


1.function test(){
2.   var date=new Date();
3.   if(date.getDay()==1&& date.getHours()+1 >=8 && date.getMinutes()+1 >=35)
4.   { //8-9AM
5.      $(“#dlg”).dialog();
6.      clearInterval(iId);
7.   }
8.   elseif(date.getDay()==1&& date.getHours()+1 == 9 && date.getMinutes()+1 <= 20)
9.   {
10.         $(“#dlg”).dialog();
11.      clearInterval(iId);
12.   }else
13.   {
14.     //nothing happens
15.   }
  • Line number 2 creates a current date instance.
  • Line number 3 checks the time, whether it is between 8:35AM and 9AM (using getHours and getMinutes) and today is Monday (using getDay, 1 for Monday).
  • If line number 3 is true, then a happy hour message is shown, else line number 8 is checked.
  • Line number 6 stops the checking of time as our goal to run the script is met.
  • Line number 8 checks whether the time is between 9:00 AM and 9:20 AM.



That’s all for this article. I hope you have enjoyed reading this article and learned something new from it. Don’t forget to provide your valuable inputs for this article in the comments section. In case of any doubt, feel free to ask in the comments.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Arpit Jain
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralGot some points Pin
Ambre Sachin18-Feb-14 3:36
memberAmbre Sachin18-Feb-14 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 18 Feb 2014
Article Copyright 2014 by Arpit Jain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid