JavaScript Date: Create, Convert, Compare Dates in JavaScript
JavaScript provides Date object to work with date & time, including days, months, years, hours, minutes, seconds, and milliseconds.
Use the Date()
function to get the string representation of the current date and time in JavaScript. Use the new keyword in JavaScript to get the Date object.
Date(); //Returns current date and time string
//or
var currentDate = new Date(); //returns date object of current date and time
Create a date object by specifying different parameters in the Date()
constructor function.
new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
Parameters:
- No Parameters: A date object will be set to the current date & time if no parameter is specified in the constructor.
- value: An integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC.
- dateString: A string value that will be parsed using
Date.parse()
method. - year: An integer value to represent a year of a date. Numbers from 0 to 99 map to the years 1900 to 1999. All others are actual years.
- monthIndex: An integer value to represent a month of a date. It starts with 0 for January till 11 for December
- day: An integer value to represent day of the month.
- hours: An integer value to represent the hour of a day between 0 to 23.
- minutes: An integer value to represent the minute of a time segment.
- seconds: An integer value to represent the second of a time segment.
- milliseconds: An integer value to represent the millisecond of a time segment. Specify numeric milliseconds in the constructor to get the date and time elapsed from 1/1/1970.
In the following example, a date object is created by passing milliseconds in the Date()
constructor function. 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
The following example shows various formats of a date string that can be specified 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 the 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 the specified year, month and optionally date, hours, minutes, seconds and milliseconds.
var date1 = new Date(2021, 2, 3); // Mon Feb 03 2021
var date2 = new Date(2021, 2, 3, 10); // Mon Feb 03 2021 10:00
var date3 = new Date(2021, 2, 3, 10, 30); // Mon Feb 03 2021 10:30
var date4 = new Date(2021, 2, 3, 10, 30, 50); // Mon Feb 03 2021 10:30:50
var date5 = new Date(2021, 2, 3, 10, 30, 50, 800); // Mon Feb 03 2021 10:30:50
Date Formats
JavaScript supports ISO 8601 date format by default -YYYY-MM-DDTHH:mm:ss.sssZ
var dt = new Date('2015-02-10T10:12:50.5000z');
</code></pre>
<div className="card-footer example-footer"></div></div>
</div>
<h2>Convert Date Formats</h2>
<p>
Use different Date methods to convert a date from one format to another format, e.g., to Universal Time, GMT, or local time format.
</p>
<p>
The following example demonstrates <code>ToUTCString()</code>, <code>ToGMTString()</code>, <code>ToLocalDateString()</code>, and <code>ToTimeString()</code> methods to convert date into respective formats.
</p> <div className="card code-panel">
<div className="card-header example-title">Example: Date Conversion in Different Formats</div>
<div className="panel-body"><pre className="csharpcode"><code>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 object methods. The following example converts a 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 third party JavaScript Date library like datejs.com or momentjs.com to work with Dates extensively in JavaScript.
Compare Dates in JavaScript
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);