JavaScript & jQuery Tutorials

JS Home Right Arrow JS Intermediate Right Arrow Dates and Times

Dates and Times

Dates are JavaScript objects we can create to hold date and time information. In this lesson we look at some of the ways we can create and use Date objects.

By using methods of the Date object we can manipulate dates and times after creation. There are some things to be aware of before we start delving into date creation and use some of the methods available.

  • The Computer Date and Time started on 1 January 1970 00:00:00 UTC (Universal Time Coordinated) and so date creation using the milliseconds parameter starts from this date.
  • Hours are specified using a 24 hour clock.
  • Days of the week are specified using the numbers 0-6. 0 = Sunday, 1 = Monday etc.
  • Months of the year are specified using the numbers 0-11. 0 = January, 1 = February etc.
  • Ensure you always use four digits when supplying a year to avoid issues like Y2K.

Date Creation

There are numerous ways to create Date objects, all of which are covered in the reference section. See the code below for examples of some of them.



// Create a Date instance for the current date and time.
var aDate = new Date();
alert(aDate);

// Create a Date instance using the milliseconds parameter.
var bDate = new Date(300000000);
alert(bDate);

// Create a Date instance using a negative milliseconds parameter.
var cDate = new Date(-300000000);
alert(cDate);

// Parse a Date.
var dDate = new Date(Jan 1, 2000);
alert(dDate);

Press the button below to action the above code:



Extracting Date Information

There are lot of getter methods asscoiated with the Date object that allows us to extract information from a date. See the code below for examples of some of them.



// Create a Date instance for the current date and time.
var aDate = new Date();
alert(aDate);

// Get Year.
alert(aDate.getFullYear());

// Get Day of the month.
alert(aDate.getDate());

Press the button below to action the above code:



Date Calculation

As mentioned above computer dates start from 1 January 1970 00:00:00 UTC. There is even a date routine that gives us the number of milliseconds that have elapsed since then. This comes in very handy when we want to calculate future dates. See the code below for an example of date calculation.



// Create a Date instance for the current date and time.
var aDate = new Date();
alert(aDate);

// Get number of milliseconds up till now.
var aDateInMilliseconds = aDate.getTime();

// Calcualate milliseconds in a fortnight (14 days).
var aFortnight = 1000*60*60*24*14;

// Get Date in a fortnight.
var twoWeeksFromNow = new Date(aDateInMilliseconds + aFortnight);
alert(twoWeeksFromNow);

Press the button below to action the above code:



Lesson 2 Complete

In this lesson we looked at dates and some of the methods used for manipulating them.

For a complete list of the methods available with the Date object and their usage visit the reference section of the site.


What's Next?

In the next lesson we look at basic conditional statements and how to use them.


Reference

JavaScript Reference - Date constructor
JavaScript Reference - new special operator



  

go to home page Homepage go to top of page Top