Date in JavaScript:

JavaScript provides Date object to work with date & time including days, months, years, hours, minutes, seconds and milliseconds.

The following example shows how to display current date and time using Date object in JavaScript.

Example: Current Local Date

Date(); //current date

//or

var currentDate = new Date(); //current date

As you can see in the above example, we can display current date and time either by calling Date as function or creating an object with new keyword.

In order to work with date other than current date and time, we must create a date object by specifying different parameters in the Date constructor.

Syntax:
var dt = new Date();

var dt = new Date(milliseconds);

var dt = new Date('date string');

var dt = new Date(year, month[, date, hour, minute, second, millisecond]);

As per the above syntax, the following parameters can be specified in Date constructor.

  • No Parameter: Date object will be set to current date & time if no parameter is specified in the constructor.
  • Milliseconds: Milliseconds can be specified as numeric parameter. The date object will calculate date & time by adding specified numeric milliseconds from mid night of 1/1/1970
  • Date string: String parameter will be treated as a date and will be parsed using Date.parse method.

Overload of Date constructor includes following seven numeric parameters.

  • year: Numeric value to represent year of a date.
  • month: Numeric value to represent month of a date. Starts with 0 for January till 11 for December
  • date: Numeric value to represent day of a date (optional).
  • hour: Numeric value to represent hour of a day (optional).
  • minute: Numeric value to represent minute of a time segment (optional).
  • second: Numeric value to represent second of a time segment (optional).
  • millisecond: Numeric value to represent millisecond of a time segment(optional). Specify numeric milliseconds in the constructor to get the date and time elapsed from 1/1/1970.

In the following example, date object is created by passing milliseconds in Date constructor. So date will be calculated based on milliseconds elapsed from 1/1/1970.

Example: Create Date by specifying milliseconds

var date1 = new Date(0);  // Thu Jan 01 1970 05:30:00

var date2 = new Date(1000); // Thu Jan 01 1970 05:30:01

var date3 = new Date(5000); // Thu Jan 01 1970 05:30:05 

Specify any valid date as a string to create new date object for the specified date. The following example shows various formats of date string which you can specify in a Date constructor.

Example: Create Date object by specifying different dates as string

var date1 = new Date("3 march 2015");

var date2 = new Date("3 February, 2015");

var date3 = new Date("3rd February, 2015"); // invalid date

var date4 = new Date("2015 3 February");

var date5 = new Date("3 2015 February ");

var date6 = new Date("February 3 2015");

var date7 = new Date("February 2015 3");

var date8 = new Date("2 3 2015");

var date9 = new Date("3 march 2015 20:21:44");

You can use any valid separator in date string to differentiate date segments.

Example: Create Date object using different separator
            
var date1 = new Date("February 2015-3");

var date2 = new Date("February-2015-3");

var date3 = new Date("February-2015-3");

var date4 = new Date("February,2015-3");

var date5 = new Date("February,2015,3");

var date6 = new Date("February*2015,3");

var date7 = new Date("February$2015$3");

var date8 = new Date("3-2-2015"); // MM-dd-YYYY

var date9 = new Date("3/2/2015"); // MM-dd-YYYY

Specify seven numeric values to create a date object with specified year, month and optionally date, hours, minutes, seconds and milliseconds.

Example: Date
           
var dt = new Date(2014, 2, 3, 10, 30, 50, 800); // Mon Mar 03 2014 10:30:50 

Date Methods:

The JavaScript Date object includes various methods to operate on it. Use different methods to get different segments of date like day, year, month, hour, seconds or milliseconds in either local time or UTC time.

Example: Date methods

var date = new Date('4-1-2015');

date.getDay();// returns 0

date.getYear();// returns 115, no of years after 1900

date.getFullYear();// returns 2015 

date.getMonth();// returns 3, starting 0 with jan 

date.getUTCDate();// returns 31 

Visit Date Methods Reference to know about all the date methods.

Date Formats:

JavaScript supports ISO 8601 date format by default - YYYY-MM-DDTHH:mm:ss.sssZ

Example: ISO Date Format

var dt = new Date('2015-02-10T10:12:50.5000z');

Convert Date format:

Use different Date methods to convert a date from one format to another format e.g. to Universal Time, GMT or local time format.

For example, use ToUTCString(), ToGMTString(), ToLocalDateString(), ToTimeString() methods to convert date into respective formats.

Example: Date conversion in different formats

var date = new Date('2015-02-10T10:12:50.5000z');

date; 'Default format:'

date.toDateString();'Tue Feb 10 2015'

date.toLocaleDateString();'2/10/2015'

date.toGMTString(); 'GMT format' 

date.toISOString(); '2015-02-10T10:12:50.500Z' 

date.toLocaleString();'Local date Format '

date.toLocaleTimeString(); 'Locale time format '

date.toString('YYYY-MM-dd'); 'Tue Feb 10 2015 15:42:50'

date.toTimeString(); '15:42:50' 

date.toUTCString(); 'UTC format ' 

To get date string in formats other than the ones listed above, you need to manually form the date string using different Date methods. The following example converts date string to DD-MM-YYYY format.

Example: Get Date segments
            
var date = new Date('4-1-2015'); // M-D-YYYY

var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();

var dateString = (d <= 9 ? '0' + d : d) + '-' + (m <= 9 ? '0' + m : m) + '-' + y;

 
Note : Use third party JavaScript Date library like datejs.com or momentjs.com, if you want to work with Dates extensively.

Parse date:

Use Date.parse() method to convert valid date string into milliseconds since midnight of 1/1/1970.

Example: Date.parse()

Date.parse("5/2/2015"); // 1430505000000

var date = new Date(Date.parse("5/2/2015")); // Sat May 02 2015 00:00:00

Compare Dates:

Use comparison operators to compare two date objects.

Example: Date Comparison

var date1 = new Date('4-1-2015');
var date2 = new Date('4-2-2015');

if (date1 > date2)
    alert(date1 + ' is greater than ' + date2);
else (date1 < date2 )
    alert(date1 + ' is less than ' + date2);

Points to Remember :

  1. Get current date using Date() or new Date().
  2. Date object can be created using new keyword. e.g. var date = new Date();
  3. Date can be created by specifying milliseconds, date string or year and month in Date constructor.
  4. Date can be created by specifying date string in different formats using different separators.
  5. Date methods are used to perform different tasks on date objects.