JavaScript(JS) Date and Time
Creating Date Objects
In JavaScript, you can create a new Date
object to represent a specific date and time. Here are some examples of how to create Date
objects:
- Creating a
Date
object for the current date and time:
const now = new Date();
In this example, the now
variable will contain a Date
object representing the current date and time.
- Creating a
Date
object for a specific date and time:
const myDate = new Date("2023-02-23T12:00:00");
In this example, the myDate
variable will contain a Date
object representing February 23, 2023 at 12:00:00.
- Creating a
Date
object for a specific date and time using separate arguments:
const myDate = new Date(2023, 1, 23, 12, 0, 0);
In this example, the myDate
variable will contain a Date
object representing February 23, 2023 at 12:00:00.
The Date
object provides several methods to retrieve different parts of the date and time, such as getFullYear()
, getMonth()
, getDate()
, getHours()
, getMinutes()
, getSeconds()
, and getMilliseconds()
. Here's an example:
const now = new Date(); console.log(now.getFullYear()); // e.g. 2023 console.log(now.getMonth()); // e.g. 1 (February) console.log(now.getDate()); // e.g. 23 console.log(now.getHours()); // e.g. 10 (depending on your time zone) console.log(now.getMinutes()); // e.g. 30 console.log(now.getSeconds()); // e.g. 0 console.log(now.getMilliseconds()); // e.g. 500
In this example, the now
variable contains a Date
object representing the current date and time. The different methods are used to retrieve the year, month, date, hours, minutes, seconds, and milliseconds of the date and time stored in the now
object.
new Date()
In JavaScript, the Date
object is used to work with dates and times. The new Date()
constructor can be used to create a new Date
object representing the current date and time, or a specific date and time.
Here are some examples of how to use new Date()
:
- Creating a
Date
object for the current date and time:
const now = new Date();
In this example, the now
variable will contain a Date
object representing the current date and time.
- Creating a
Date
object for a specific date and time:
const myDate = new Date("2023-02-23T12:00:00");
In this example, the myDate
variable will contain a Date
object representing February 23, 2023 at 12:00:00.
- Creating a
Date
object for a specific date and time using separate arguments:
const myDate = new Date(2023, 1, 23, 12, 0, 0);
In this example, the myDate
variable will contain a Date
object representing February 23, 2023 at 12:00:00.
The Date
object provides several methods to retrieve different parts of the date and time, such as getFullYear()
, getMonth()
, getDate()
, getHours()
, getMinutes()
, getSeconds()
, and getMilliseconds()
. Here's an example:
const now = new Date(); console.log(now.getFullYear()); // e.g. 2023 console.log(now.getMonth()); // e.g. 1 (February) console.log(now.getDate()); // e.g. 23 console.log(now.getHours()); // e.g. 10 (depending on your time zone) console.log(now.getMinutes()); // e.g. 30 console.log(now.getSeconds()); // e.g. 0 console.log(now.getMilliseconds()); // e.g. 500
In this example, the now
variable contains a Date
object representing the current date and time. The different methods are used to retrieve the year, month, date, hours, minutes, seconds, and milliseconds of the date and time stored in the now
object.
new Date(milliseconds)
In JavaScript, the Date
object can also be used to work with dates and times in milliseconds. The new Date()
constructor can accept a numeric value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC (also known as the Unix Epoch) to create a new Date
object representing a specific date and time.
Here's an example of how to create a Date
object using milliseconds:
const myDate = new Date(1614096000000);
In this example, the myDate
variable will contain a Date
object representing the date and time that corresponds to the Unix timestamp 1614096000000
, which represents March 1, 2021 at 00:00:00 UTC.
You can also use the getTime()
method of a Date
object to retrieve the number of milliseconds between the date and time represented by the Date
object and the Unix Epoch. Here's an example:
const myDate = new Date("2023-02-23T12:00:00"); const milliseconds = myDate.getTime(); console.log(milliseconds); // e.g. 1687785600000
In this example, the myDate
variable contains a Date
object representing February 23, 2023 at 12:00:00. The getTime()
method is used to retrieve the number of milliseconds since the Unix Epoch that corresponds to this date and time, which is then stored in the milliseconds
variable.
new Date(date string)
In JavaScript, the new Date()
constructor can also accept a date string as an argument to create a new Date
object representing a specific date and time.
Here's an example of how to create a Date
object using a date string:
const myDate = new Date("2023-02-23T12:00:00");
In this example, the myDate
variable will contain a Date
object representing February 23, 2023 at 12:00:00.
The date string should be in the ISO format YYYY-MM-DDTHH:mm:ss.sssZ
, where:
YYYY
is the four-digit yearMM
is the two-digit month (01 for January, 02 for February, and so on)DD
is the two-digit day of the monthT
is the separator between the date and time componentsHH
is the two-digit hour (00 for midnight, 01 for 1 AM, and so on)mm
is the two-digit minute (00 through 59)ss
is the two-digit second (00 through 59).sss
is the three-digit millisecond (000 through 999)Z
is the optional time zone offset. If omitted, the time is assumed to be in the local time zone. If specified, it should be in the format±HH:mm
, where±
is either+
or-
, andHH
andmm
are two-digit values representing the hours and minutes of the offset.
You can also use the toISOString()
method of a Date
object to convert it to an ISO-formatted date string. Here's an example:
const myDate = new Date("2023-02-23T12:00:00"); const dateString = myDate.toISOString(); console.log(dateString); // "2023-02-23T12:00:00.000Z"
In this example, the myDate
variable contains a Date
object representing February 23, 2023 at 12:00:00. The toISOString()
method is used to convert this Date
object to an ISO-formatted date string, which is then stored in the dateString
variable.
JavaScript Date Methods
In JavaScript, the Date
object provides a number of methods for working with dates and times. Here are some of the most commonly used Date
methods:
getDate()
: returns the day of the month (1-31) for the specified dategetMonth()
: returns the month (0-11) for the specified dategetFullYear()
: returns the four-digit year for the specified dategetHours()
: returns the hour (0-23) for the specified dategetMinutes()
: returns the minute (0-59) for the specified dategetSeconds()
: returns the second (0-59) for the specified dategetMilliseconds()
: returns the millisecond (0-999) for the specified dategetTime()
: returns the number of milliseconds since January 1, 1970, 00:00:00 UTC (also known as the Unix Epoch) for the specified dategetTimezoneOffset()
: returns the difference between the local time zone and UTC time, in minutessetDate()
: sets the day of the month (1-31) for the specified datesetMonth()
: sets the month (0-11) for the specified datesetFullYear()
: sets the four-digit year for the specified datesetHours()
: sets the hour (0-23) for the specified datesetMinutes()
: sets the minute (0-59) for the specified datesetSeconds()
: sets the second (0-59) for the specified datesetMilliseconds()
: sets the millisecond (0-999) for the specified datetoISOString()
: returns the date and time in the ISO formatYYYY-MM-DDTHH:mm:ss.sssZ
toString()
: returns a string representation of the date and time
Here's an example of how to use some of these methods:
const myDate = new Date("2023-02-23T12:00:00"); console.log(myDate.getFullYear()); // 2023 console.log(myDate.getMonth()); // 1 (February) console.log(myDate.getDate()); // 23 console.log(myDate.getHours()); // 12 console.log(myDate.getMinutes()); // 0 console.log(myDate.getSeconds()); // 0 console.log(myDate.getTimezoneOffset()); // -300 (UTC-05:00)
In this example, the myDate
variable contains a Date
object representing February 23, 2023 at 12:00:00. The getFullYear()
, getMonth()
, getDate()
, getHours()
, getMinutes()
, getSeconds()
, and getTimezoneOffset()
methods are used to retrieve various properties of the Date
object.
Formatting a Date
In JavaScript, you can format a Date
object into a string using various methods. Here are some commonly used methods for formatting dates:
toLocaleString()
: This method converts aDate
object to a string, using the browser's locale and time zone settings.
const myDate = new Date(); console.log(myDate.toLocaleString()); // Output: 2/23/2023, 3:25:00 PM
toUTCString()
: This method converts aDate
object to a string, in the formatddd, dd MMM yyyy HH:mm:ss GMT
, which is the UTC time zone.
const myDate = new Date(); console.log(myDate.toUTCString()); // Output: Fri, 23 Feb 2023 20:26:45 GMT
toISOString()
: This method converts aDate
object to a string, in the formatYYYY-MM-DDTHH:mm:ss.sssZ
.
const myDate = new Date(); console.log(myDate.toISOString()); // Output: 2023-02-23T20:28:57.718Z
- Using string concatenation: You can also use string concatenation to build your own custom date format, using the
getFullYear()
,getMonth()
,getDate()
,getHours()
,getMinutes()
, andgetSeconds()
methods.
const myDate = new Date(); const formattedDate = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate() + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds(); console.log(formattedDate); // Output: 2023-2-23 20:31:21
In this example, we're building a custom format that includes the year, month, day, hours, minutes, and seconds. We add 1 to the getMonth()
method's return value because it returns a zero-based index (0 for January, 1 for February, etc.).
AutoCorrection in Date Object
In JavaScript, the Date
object automatically corrects invalid date values. For example, if you create a Date
object with a month value of 13, it will automatically correct it to January of the next year. Similarly, if you create a Date
object with a day value that is greater than the number of days in the month, it will automatically correct it to the next month.
Here's an example:
const myDate = new Date(2023, 13, 32); // February 1st, 2024 console.log(myDate); // Output: Fri Feb 01 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
In this example, we're creating a Date
object with a year of 2023, a month of 13, and a day of 32. Since there is no 13th month, and February only has 28 or 29 days, the Date
object automatically corrects the values to February 1st, 2024.
Similarly, if we create a Date
object with a day value that is greater than the number of days in the month, it will automatically correct it to the next month:
const myDate = new Date(2023, 1, 32); // March 4th, 2023 console.log(myDate); // Output: Sun Mar 04 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
In this example, we're creating a Date
object with a year of 2023, a month of 1 (February), and a day of 32. Since February only has 28 or 29 days, the Date
object automatically corrects the day value to March 4th, 2023.