Working with dates and time

JavaScript provides an easy way for getting current day and time. For this you just need to create an object Date() by using the function new and assign a reference to this object to a variable. For example,

var today = new Date()

sets up the variable today with current date and time. The current date and time is taken from the hardware settings of your computer.

The time is stored as the number of milliseconds since 6pm, on December 31, 1969. In order to convert this number into current date/time, you can use the methods shown below. Note that the returned value for January is 0, so you need to add 1 to it to get the actual month number.

JavaScript codeCommentsResult
today = new Date(); create the Date object
time = today.getTime(); retrieve current time  
year = today.getFullYear(); retrieve a 4-digit year  
month = today.getMonth()+1; retrieve current month  
date = today.getDate(); retrieve current date  
day = today.getDay(); retrieve current day  
hours = today.getHours(); retrieve the hour  
minutes = today.getMinutes(); retrieve the minutes  
seconds = today.getSeconds(); retrieve the seconds  

Using these functions it is easy to implement automatic setting of the current date in your Web page. The date will be recomputed every time the Web page is loaded.

To achieve this just add the following code to the appropriate place in your Web page:

JavaScript codeComments
<script type="text/javascript">
var ddd = new Date();
var month = ddd.getMonth()+1;
document.write("<b>Today's date: " + 
  month + "/" + ddd.getDate() +
  "/" + ddd.getFullYear() + "</b>");
</script>
opening JavaScript
creating the Data object ddd
retriving the actual month
write "Today's date" in bold
add month/day
add /year and close writing
close JavaScript

Date/time calculator

You can create not just the current time object, but also a time object corresponding to the number of milliseconds passed since the epoh day above. Use the following format:

var today = new Date(msec)

sets up the variable today with date and time corresponding to the value of msec. Beware that the date/time created this way will depend on the time zone where your computer is located.

JavaScript codeCommentsResult
today = new Date(time); create the Date object  
time = today.getTime(); retrieve current time  
year = today.getFullYear(); retrieve a 4-digit year  
month = today.getMonth()+1; retrieve current month  
date = today.getDate(); retrieve current date  
day = today.getDay(); retrieve current day  
hours = today.getHours(); retrieve the hour  
minutes = today.getMinutes(); retrieve the minutes  
seconds = today.getSeconds(); retrieve the seconds  

Similarly one can create the time objects corresponding to a specific date and time by passing the following parameter combinations to its constructor:

var timeStamp = new Date(year, month, day);
var timeStamp = new Date(year, month, day, hours, minutes, seconds);
var timeStamp = new Date("March 5, 1986, 12:23:34");