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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read