JavaScript Date: Create, Convert, and Compare Dates Easily

Explore how to use JavaScript's Date object to create, convert, and compare dates and times. Learn how to handle days, months, years, hours, minutes, seconds, and milliseconds in your JavaScript applications for accurate date and time manipulation.



JavaScript Date

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

Example: Date In JavaScript

Syntax

Date(); //Returns current date and time string

// or

var currentDate = new Date(); //returns date object of current date and time
Output

Current date and time string or Date object

Example: Create Date by Specifying Milliseconds

Syntax

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 
Output

Thu Jan 01 1970 05:30:00
Thu Jan 01 1970 05:30:01
Thu Jan 01 1970 05:30:05

Example: Create Date by Specifying Date String

Syntax

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");
Output

Mon Mar 03 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Feb 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Invalid Date
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 03 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Feb 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Feb 03 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Feb 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 03 2015 20:21:44 GMT+0000 (Coordinated Universal Time)

Example: Create Date using Different Date Separator

Syntax

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
Output

Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Mar 02 2015 00:00:00 GMT+0000 (Coordinated Universal Time)

Example: Date

Syntax

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
Output

Mon Feb 03 2021 00:00:00 GMT+0000 (Coordinated Universal Time)
Mon Feb 03 2021 10:00:00 GMT+0000 (Coordinated Universal Time)
Mon Feb 03 2021 10:30:00 GMT+0000 (Coordinated Universal Time)
Mon Feb 03 2021 10:30:50 GMT+0000 (Coordinated Universal Time)
Mon Feb 03 2021 10:30:50 GMT+0000 (Coordinated Universal Time)

Example: ISO Date Format

Syntax

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

Wed Feb 10 2015 10:12:50 GMT+0000 (Coordinated Universal Time)

Example: Date Conversion in Different Formats

Syntax

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'
Output

Tue Feb 10 2015 10:12:50 GMT+0000 (Coordinated Universal Time)
Tue Feb 10 2015
2/10/2015
Wed, 10 Feb 2015 10:12:50 GMT
2015-02-10T10:12:50.500Z
2/10/2015, 10:12:50 AM
10:12:50 AM
Tue Feb 10 2015 10:12:50 GMT+0000 (Coordinated Universal Time)
15:12:50
Tue, 10 Feb 2015 10:12:50 GMT

Example: Get Date Segments

Syntax

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;
Output

04-01-2015

Example: Date Comparison

Syntax

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

if (date1 > date2) {
    alert(date1 + ' is greater than ' + date2);
} else {
    alert(date1 + ' is less than ' + date2);
}
Output

Alert box showing which date is greater