This article explains how to obtain the current date and time and specified date and time using JavaScript.

How can I get the date and time?



To handle dates and times, use the Date object .
Let’s check how to use the Date object, time format conversion, etc.
Date object handling date and time
To handle dates and times, use the Date object .
new Date([date/time])
Date objects are used to represent the current time or a specific date and time. By using the Date object, you can get the current time or specify the time.
How to get the current time
var date = new Date();
console.log(date);
How to specify date and time using a string
You can also retrieve the date and time by passing a string representing the date and time.
var date = new Date("2022-12-20");
console.log(date);
How to specify date and time numerically
Specify the year, month, day, hour, minute, second, and millisecond.
var date = new Date(2022, 11, 20 , 11 , 10 , 55 , 500); //Date, hour, minute, second, millisecond
console.log(date);
When using the Date object, we recommend that you check its operation, as it may behave differently depending on the browser or environment.
summary
Below is a summary of how to handle dates.
- Handling dates and times using Date objects
- You can specify the current time or a specific date and time to retrieve it.



Learning how to use the Date object was very helpful!



The date and time obtained will differ depending on the specification method, so please be careful when using it.
Comments