JavaScript Date

About JavaScript Date

JavaScript's Date is an object which is used to work with dates and times.

It provides methods to get, set, and manipulate date values.

Creating a Date Object

A Date object can be created using the new Date() constructor in the following ways:

Create Current Date and Time

new Date() returns the current date and time according to the system's local time zone.

Create a Specific Date

A specific date can be created by passing the year, month, day as arguments to the Date constructor.

The month is zero-based, which means January is 0, February is 1, March is 2, and so on.

The day is one-based, which means the first day of the month is 1, the second day is 2, and so on.

Numeric date format : new Date(year, month, day) (e.g. new Date(2025, 2, 28) refers to March 28, 2025)

Create a Specific Date Time

A specific date time can be created by passing the year, month, day, hours, minutes, seconds, milliseconds as arguments to the Date constructor.

Numeric date time format : new Date(year, month, day, hours, minutes, seconds, milliseconds) (e.g. new Date(2025, 2, 28, 15, 30, 20, 300) refers to March 28, 2025 15:30:20:300 )

Create a Date from a String

The safest and most precise way to create a date from a string is using the ISO 8601 format:

  • YYYY-MM-DD (e.g. "2025-03-28")
  • YYYY-MM-DDTHH:mm:ss (e.g. "2025-03-28T15:30:50")
  • YYYY-MM-DDTHH:mm:ss.sssZ (e.g. "2025-03-28T15:30:20Z")

JavaScript can also parse a variety of human-readable date strings but it is less reliable.

  • "MMM DD, YYYY" (e.g., "Mar 28, 2025")
  • "Month DD, YYYY" (e.g., "March 28, 2025")
  • "Month DD, YYYY HH:mm:ss" (e.g., "March 28, 2025 15:30:20"))

Unix Timestamp Format Using Milliseconds Since 1970

In JavaScript, the Unix timestamp in milliseconds represents the number of milliseconds that have elapsed since the Unix Epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC).

Unix Epoch: The Unix Epoch is the reference point (January 1, 1970, 00:00:00 UTC).

Getting Date and Time Components

The JavaScript Date object provides different methods to extract specific parts of a date.

Get the Year, Month, and Day

  • getFullYear() - Returns the year (4 digits)
  • getMonth() - Returns the month (0-11)
  • getDate() - Returns the day of the month (1-31)

Get the Hours, Minutes, Seconds, and milliseconds

  • getHours() - Returns the hour (0-23)
  • getMinutes() - Returns the minutes (0-59)
  • getSeconds() - Returns the seconds (0-59)
  • getMilliseconds() - Returns the milliseconds (0-999)

Get the Day of the Week

  • getDay() - Returns the day of the week (0-6)

The day of the week is returned as a number (0-6), where 0 represents Sunday, 1 represents Monday, and so on.

Setting Date and Time

>

Date and time components of a date can be modified using different setter methods:

  • setFullYear(year) - Sets the year (4 digits)
  • setMonth(month) - Sets the month (0-11)
  • setDate(day) - Sets the day of the month (1-31)
  • setHours(hours) - Sets the hour (0-23)
  • setMinutes(minutes) - Sets the minutes (0-59)
  • setSeconds(seconds) - Sets the seconds (0-59)
  • setMilliseconds(milliseconds) - Sets the milliseconds (0-999)

Formatting a Date

>

JavaScript date object has different methods to convert a date to a string and custom formatting:

Convert a Date to a String

  • toString() - Returns a string representation of the date (Example output: Tue Mar 25 2025 15:20:30 GMT... (your time zone))
  • toDateString() - Returns a string representation of the date without time (Example output: Mon Mar 25 2025)
  • toTimeString() - Returns a string representation of the time without date (Example output: 15:20:30 GMT... (Your time zone))
  • toUTCString() - Returns a string representation of the date in UTC (Example output: Mon, 25 Mar 2025 15:20:30 GMT)
  • toISOString() - Returns a string representation of the date in ISO format (Example output: 2025-03-25T15:20:30.000Z)
  • toLocaleString() - Returns a string representation of the date (full date and time) in the local format (Example output: 3/25/2025, 3:20:30 PM)
  • toLocaleDateString() - Returns a string representation of the date (only date) in the local date format (Example output: 3/25/2025)
  • toLocaleTimeString() - Returns a string representation of the date (only time) in the local time format (Example output: 3:20:30 PM)

Time zone of the output will vary depending on your time zone. UTC date string and ISO date string outputs will also vary depending on your time zone.

The toLocaleString() method can take two arguments: locale and options. The date can be customized to a local format using lacale and options arguments.

  • locale - Specify the locale (e.g. 'en-US', 'en-GB', 'ar-SA', 'DE', 'bn-BD', 'hi-IN', 'ur-PK', 'ru-RU', 'zh-CN')
  • options - Specify the options (e.g. { weekday: "long", year: "numeric", month: "long", day: "numeric" })

Custom Formatting

A date format can be customized using Intl.DateTimeFormat object by specifying various options, such as: year, month, day, hour, minute, second, weekday, timeZone, dateStyle, timeStyle, calendar, numberingSystem.

  • weekday - Specify the day of the week format (e.g. 'short' or 'long')
  • year - Specify the year format (e.g. 'numeric', or '2-digit')
  • month - Specify the month format (e.g. 'numeric', 'short' or 'long')
  • day - Specify the day of the month format (e.g. 'numeric' or '2-digit')
  • hour - Specify The hour format (e.g. 'numeric', or '2-digit')
  • minute - Specify the minute format (e.g. 'numeric', or '2-digit')
  • second - Specify the second format (e.g. 'numeric', or '2-digit')
  • timeZone - Specify the time zone (e.g. 'Europe/London')
  • dateStyle - Specify the date style (e.g. 'full', 'long', 'medium', or 'short')
  • timeStyle - Specify the time style (e.g. 'long', 'medium', or 'short')
  • calendar - Specify the calendar (e.g. 'gregory', 'islamic-umalqura', 'japanese', etc.)
  • numberingSystem - Specify the numbering system (e.g. 'latn', 'arab', 'german', etc.)
  • hour12 - Specify the 24 hour format (e.g. true or false)

Examples of Some Custom Formatting

JavaScript Compiler For Live Practice

Practice the above JavaScript code in the following JavaScript Compiler. Create more dates and try different Date methods in the following editor and click Execute button to check the output. The output will be displayed in JavaScript Code Output section.

JavaScript logo JavaScript Compiler
JavaScript Code Output