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.
<script>
Date(); //current date
//or
var currentDate = new Date(); //current date
</script>
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.
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]);
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.
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.
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.
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.
var dt = new Date(2014, 2, 3, 10, 30, 50, 800); // Mon Feb 03 2014 10:30:50
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.
var date = new Date('4-1-2015');
date.getDay();// returns 3
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
Method | Description |
---|---|
Date() | It returns presents day’s date and time. |
getDate() | It returns the day for the specified date. |
getDay() | It returns the day of the week for the specified date. |
getFullYear() | It returns the year of the specified date. |
getYear() | This method returns no. of years after 1900 for specified date. |
getHours() | It returns the hour in a specified date. |
getMilliseconds() | It returns the milliseconds in the specified date. |
getMinutes() | It returns the minutes in the specified date. |
getMonth() | It returns the month in the specified date. This also find the month. |
getSeconds() | This method returns the seconds in the specified date. |
getTime() | This method returns the date in terms of numeric value as milliseconds. |
setDate() | This method sets the day of the month for a specified date. |
setFullYear() | This method sets the full year for a specified date. |
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.
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.
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;
Use Date.parse() method to convert valid date string into milliseconds since midnight of 1/1/1970.
Date.parse("5/2/2015"); // 1430505000000
var date = new Date(Date.parse("5/2/2015")); // Sat May 02 2015 00:00:00
Use comparison operators to compare two date objects.
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);
This article is contributed by Anmol. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.