Introduction
In web development, getting the current date is a common task. Fortunately, JavaScript provides a built-in Date object that can be used to easily retrieve the current date and time. In this article, we will explore how to get the current date and time in JavaScript using the Date object, as well as some useful methods for working with dates and times in JavaScript. Whether you are building a web app, a mobile app, or any other type of application in JavaScript, understanding how to work with dates and times is an essential skill. So let's dive in and learn how to get the current date and time in JavaScript.
Using the Date()
method
In JavaScript, dates are represented by the Date
object, which provides a wide range of methods for working with dates and times. To create a new Date
object, you can use the new Date()
constructor, which creates a new Date
object representing the current date and time:
The Date()
method is used to create a new date object. When called without any arguments, it returns the current date and time. Here is the simple way to create a current date and time.
const currentDate = new Date();
Get Year using the getFullYear()
method
The getFullYear()
method returns the year of the current date as a four-digit number.
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
console.log(currentYear);
Output
2023
Get Month using the getMonth()
method
The getMonth()
method returns the month of the current date as a zero-based number. This means that January is represented by 0, February by 1, and so on.
const currentDate = new Date();
const currentMonth = currentDate.getMonth() + 1;
console.log(currentMonth);
Output
3
The code output which represents March
Get Day using the getDate()
method
The getDate()
method returns the day of the current date as a number between 1 and 31.
const currentDate = new Date();
const currentDay = currentDate.getDate();
console.log(currentDay);
Output
1
Using new Date().getTime()
The new Date().getTime()
method returns the current timestamp as a number representing the number of milliseconds since January 1, 1970, 00:00:00 UTC.
For example, if you were to run the following code:
const currentTimeStamp = new Date().getTime();
console.log(currentTimeStamp);
You might see something like this in the console output:
1647313465259
This number represents the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 UTC, up to the current moment in time. This format is commonly used for representing dates and times in computer systems, as it provides a standard way of representing time that can be easily converted and compared between different systems and programming languages.
Using the toLocaleDateString()
method
The toLocaleDateString()
method returns a string that represents the current date in a format that is appropriate for the locale of the user's browser. This will format the current date and time into a human-readable string. Here is an example of how to use the toLocaleDateString()
method
const currentDate = new Date();
const currentDateStr = currentDate.toLocaleDateString();
console.log(currentDateStr);
Output
3/1/2023
Using the toJSON()
method
The toJSON()
method returns a string that represents the current date in the ISO format.
const currentDate = new Date();
const currentDateStr = currentDate.toJSON();
console.log(currentDateStr);
Output
2023-03-01T17:53:04.840Z
Using new Date().toISOString()
The new Date().toISOString()
method returns a string representation of the current date and time in the ISO format.
The ISO format is a standard way of representing dates and times in a machine-readable format that is easy to parse and compare across different systems and programming languages. The ISO format looks like this:
YYYY-MM-DDTHH:mm:ss.sssZ
where:
YYYY
is the four-digit yearMM
is the two-digit month (01-12)DD
is the two-digit day of the month (01-31)T
is the separator between the date and the timeHH
is the two-digit hour (00-23)mm
is the two-digit minute (00-59)ss
is the two-digit second (00-59)sss
is the three-digit millisecond (000-999)Z
is the timezone offset in the format±hh:mm
So, for example, if you were to run the following code:
const currentDate = new Date().toISOString();
console.log(currentDate);
You might see something like this in the console output:
2023-03-14T03:52:39.457Z
This string represents the current date and time in the ISO format, where "2023-03-14" represents the date (March 14, 2023), "03:52:39.457" represents the time (3:52:39 and 457 milliseconds), and "Z" represents the timezone offset (which in this case is UTC).
Using the Intl.DateTimeFormat
format
If you need more control over the date formatting, you can use the Intl.DateTimeFormat
object, which provides a flexible way to format dates and times using different locales and formatting options:
const currentDate = new Date();
let formatter = new Intl.DateTimeFormat("en-US", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
timeZone: "America/New_York",
});
let formattedDate = formatter.format(currentDate);
console.log(formattedDate);
Output
Wednesday, March 1, 2023 at 12:54:32 PM
In this example, we create a DateTimeFormat
object with options that specify a long weekday name, a numeric year, a long month name, a numeric day, and numeric hour, minute, and second values, all formatted using the en-US locale. We also set the time zone to America/New_York. Finally, we call the format()
method of the DateTimeFormat
object to format the current date and time into a string using the specified formatting options.
Summary
In this article, we have explored some of the most common ways to retrieve the current date using JavaScript. The Date()
object provides various methods that can be used to get different parts of the date and time. The getFullYear()
, getMonth()
, getDate()
, toLocaleDateString()
, and toJSON()
methods are some of the most commonly used methods to retrieve the current date in JavaScript.
References
Date - JavaScript | MDN (mozilla.org)
How do I get the current date in JavaScript? - Stack Overflow