Lesson 15 of 30 – JavaScript Date
40%

JavaScript Date

JavaScript provides the Date object to work with dates and times. It can store years, months, days, hours, minutes, seconds, and milliseconds.

Note: The Date object is useful for displaying dates, calculating time differences, and managing calendars.
What is JavaScript Date?

The Date object represents a single moment in time.

  • Current date.
  • Current time.
  • Past dates.
  • Future dates.
  • Date calculations.
Example:
Date();

var currentDate = new Date();
Creating Date Objects

A Date object can be created in several ways.

new Date();
new Date(value);
new Date(dateString);
new Date(year,month);
new Date(year,month,day);
new Date(year,month,day,hour,minute,second,millisecond);

Different constructors allow different date formats.

Date Constructor Parameters
Parameter Description
No Parameter Current date and time.
Value Milliseconds since 1 Jan 1970.
Date String Date in text format.
Year Year value.
Month 0 to 11.
Day Day of month.
Hours 0 to 23.
Minutes 0 to 59.
Seconds 0 to 59.
Milliseconds 0 to 999.
Create Date using Milliseconds

Milliseconds are counted from January 1, 1970.

Example:
var date1 = new Date(0);

var date2 = new Date(1000);

var date3 = new Date(5000);

The larger the number, the later the date and time.

Create Date using String

A date can also be created using a text string.

Example:
new Date("3 March 2015");
new Date("February 3 2015");
new Date("3/2/2015");

JavaScript converts valid date strings into Date objects.

Different Date Separators

JavaScript accepts different separators while creating date strings.

Example:
new Date("February-2015-3");
new Date("February,2015,3");
new Date("3-2-2015");
new Date("3/2/2015");

Common separators include -, /, and ,.

Create Date using Numeric Values

You can specify year, month, day, hour, minute, second, and millisecond.

Example:
var date1 = new Date(2025,5,18);

var date2 = new Date(2025,5,18,10);

var date3 = new Date(2025,5,18,10,30);

var date4 = new Date(2025,5,18,10,30,45);

Remember that months start from 0 (January).

JavaScript Date Formats

JavaScript supports several date formats.

Method Purpose
toDateString() Readable date.
toLocaleDateString() Local date format.
toLocaleString() Local date and time.
toLocaleTimeString() Local time.
ISO Date Format

JavaScript supports the ISO 8601 standard format.

Example:
date.toISOString();

Output:

2025-06-18T10:30:45.000Z
Common Date Methods
Method Description
toDateString() Returns date only.
toTimeString() Returns time only.
toUTCString() Returns UTC date.
toGMTString() Returns GMT format.
toISOString() Returns ISO format.
toLocaleString() Returns local date and time.
toLocaleDateString() Returns local date.
toLocaleTimeString() Returns local time.
Advantages of Date Object
  • Stores date and time.
  • Performs date calculations.
  • Displays current time.
  • Supports different formats.
  • Useful for calendars and timers.
  • Helps calculate age and duration.
Key Points
  • Date object manages date and time.
  • Use new Date() to create a date object.
  • Months start from 0 to 11.
  • Date can be created using milliseconds.
  • Date strings are also supported.
  • JavaScript supports ISO date format.
  • Many built-in methods format dates.
  • Date methods help in web applications.
Quick Revision
Constructor Purpose
new Date() Current date and time
new Date(value) Milliseconds
new Date(string) Date from text
new Date(y,m,d) Custom date

🧠 Quick Quiz

Which statement creates the current date and time in JavaScript?