How to Log the Current Date in JavaScript ?
Last Updated :
23 Jan, 2024
The Date object is an inbuilt object constructor of JavaScript language. It is used to work with dates and times. The Date object is created by using the new keyword, i.e. new Date(). The current date can be logged on the console in JavaScript using the below-discussed methods.
Using JavaScript Date object
The Date() constructor instantiates a Date object which sets the current date and time depending on the browser’s time zone. This also comes with a wide variety of methods.
Example: The below code uses the Date object with its corresponding methods to log the current date in JavaScript.
JavaScript
// Create a new Date object
const currentDate = new Date();
const day = currentDate.getDate(),
// Since month starts from 0 (Jan)
month = currentDate.getMonth() + 1,
year = currentDate.getFullYear();
console.log(`${day}/${month}/${year}`);
Using JavaScript Date object with toString() Method
We can convert the current date to a string using the toString() method and then split it into an array of words using the space delimiter. Thereafter, the splice method is used on the array to keep the words from indices 1 to 3 i.e. the day, month and the year. Finally, the join method is used to join the resultant array.
Example: The below code uses the Date object with toString() method and its associated methods to log the current date in JavaScript.
JavaScript
// Create a new Date object
const currentDate = new Date();
console.log("Before Split: ", currentDate.toString());
console.log("After Split: ", currentDate.toString().
split(' ').splice(1, 3).join(' '));
OutputBefore Split: Tue Jan 23 2024 09:17:49 GMT+0000 (Coordinated Universal Time)
After Split: Jan 23 2024
Using JavaScript Date object with toDateString() Method
The date object comes with a method known as toDateString() which simply returns the date portion of this date interpreted in the local timezone.
Example: The below code uses the Date object with toDateString() method to log the current date in JavaScript.
JavaScript
// Create a new Date object
const currentDate = new Date();
console.log(currentDate.toDateString());
Using JavaScript Date object with toLocaleDateString() Method
The date object comes with a method known as toLocaleDateString() with parameters such as a locale which represents the location, of which the date format should be shown, and a list of options to indicate the format of how the year, month and the day of the date should be represented.
Example 1: The below code uses the Date object with toLocaleDateString() method with a specified locale (en-GB) and a list of options to represent the format of the year, the month and the day to log the current date in JavaScript.
JavaScript
const currentDate = new Date().toLocaleDateString("en-GB", {
"year": "numeric",
"month": "numeric",
"day": "numeric"
});
console.log(currentDate);
Example 2: The below code uses the Date object with toLocaleDateString() method with a specified locale (en-IN) and a list of options to represent the format of the year, the month and the day to log the current date in JavaScript.
JavaScript
const currentDate = new Date().toLocaleDateString("en-IN", {
"year": "numeric",
"month": "short",
"day": "numeric"
});
console.log(currentDate);
Using JavaScript Date object with toISOString() Method
The date object comes with a method known as toISOString() which simply returns the date in the date time string format, a format which is based on ISO 8601.
Example: The below code uses the Date object with toISOString() method to log the current date in JavaScript.
JavaScript
console.log(new Date().toISOString().split("T")[0]);
Similar Reads
How to get the current date in JavaScript ?
The Date() object is used to access the current date in JavaScript. To get the current date in string format, we can use toDateString() method. The date.toDateString() method converts the given date object into the date portion into a string. Syntax dateObj.toDateString()[GFGTABS] JavaScript // Crea
2 min read
How to Get Current Time in JavaScript ?
This article will show you how to get the current time in JavaScript. Whether you are creating a digital clock, scheduling events, or simply logging timestamps, knowing how to retrieve the current time is essential. Here, we will cover different methods to get the current time in JavaScript. Table o
3 min read
How to calculate the yesterday's date in JavaScript ?
In this article, we will see how to calculate yesterdayâs date in JavaScript. To calculate yesterday's date, you need to have some basic ideas of a few methods of JavaScript. JavaScript getDate() MethodJavaScript setDate() MethodJavaScript getDate() Method: It is an inbuilt JavaScript function that
3 min read
How to get the current year using JavaScript ?
In this article, we will know how to get the current year using the build-in method in Javascript. The purpose is to get the current year by using JavaScript getFullYear() method that will return the full year in 4-digit format for some specified date. This method is used to fetch the year from a gi
1 min read
How to Add Days to Date in JavaScript?
Adding days to a date in JavaScript is a common task when working with dates and times. This involves manipulating the Date object to calculate a future or past date by adding a specific number of days. This is useful for tasks like scheduling events, and deadlines, or simply adjusting dates dynamic
2 min read
How to convert a JavaScript date to UTC?
Javascript date can be converted to UTC by using functions present in the Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string that represents the Date based on the GMT (UT) time zone. By defa
1 min read
How to get the client timezone offset in JavaScript?
The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes. This offset is changed by dividing by 60 and negating the result. Not
1 min read
How to Convert Date to Another Timezone in JavaScript?
Converting a date to another timezone in JavaScript means adjusting the local date and time to match the time in a different timezone. This ensures that the displayed time aligns with the selected timezone, often using built-in methods or external libraries. 1. Using Intl.DateTimeFormat() and format
2 min read
JavaScript Adding hours to the Date object
Given a date, the task is to add hours to it. To add hours to date in javascript, we're going to discuss a few techniques. First few methods to know. JavaScript getHours() Method: This method returns the hour (from 0 to 23) of the provided date and time. Syntax: Date.getHours() Parameters: This meth
3 min read
How to Format Dates in JavaScript with One Line of Code?
In order to format dates, a Date object must be transformed into a string representation that follows to a predefined format. JavaScript simplifies this procedure with its built-in methods and updated APIs. We'll explore at both older, more cumbersome formats for dates as well as more recent, simple
1 min read