How to use Date object In Javascript
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}`); |
Output
23/1/2024
How to Log the Current Date in JavaScript ?
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.
Table of Content
- Using JavaScript Date object
- Using JavaScript Date object with toString() Method
- Using JavaScript Date object with toDateString() Method
- Using JavaScript Date object with toLocaleDateString() Method
- Using JavaScript Date object with toISOString() Method
Contact Us