How to Get Current Date & Time in JavaScript

Channel: Linux
Abstract: 41" To format the time in a short formator year only. Get the current date of the month. console.log(today.getDate())

Question – How do I get the current date and time in JavaScript? How to get the date in Y-m-d format in JavaScript? How do I get time in H:i:s format in JavaScript?

It’s easy to get the current date and time in JavaScript – all you need is the built-in Date object. This object provides several methods that let you manipulate dates and times, and one of those methods is called toLocaleString(). This method returns a string representation of the date and time, and it accepts an optional options object that lets you customize the format.

Get Current Date & Time in JavaScript

Getting the current date and time in JavaScript is quite simple. All you need to do is use the built-in Date object.

Here’s a quick example:

var today= new Date(); console.log(today); //Output: Thu Jun 23 2022 11:29:39 GMT+0530 (India Standard Time)12var today= new Date();console.log(today); //Output: Thu Jun 23 2022 11:29:39 GMT+0530 (India Standard Time)

This code will output the current date and time to the console. Pretty simple, right?

You can also use toLocaleString() method that returns a string with a language sensitive representation of a date. For example, to view date in 「en-US」 format, use:

var today= new Date().toLocaleString('en-US', { timeZone: 'UTC' }); console.log(today); //Ouput: 6/23/2022, 6:00:03 AM12var today= new Date().toLocaleString('en-US', { timeZone: 'UTC' });console.log(today);  //Ouput: 6/23/2022, 6:00:03 AM

Try to change the languege to 「en-GB」 and see the results:

var today= new Date().toLocaleString('en-GB', { timeZone: 'UTC' }); console.log(today); //Ouput: 23/06/2022, 06:00:2312var today= new Date().toLocaleString('en-GB', { timeZone: 'UTC' });console.log(today);  //Ouput:  23/06/2022, 06:00:23

You will find that the output is in the defined language format.

Get Current Date in JavaScript

As you can see, the default format includes the date, time, and AM/PM indicator. If you want to change any of those settings, you can do so by passing an options object to toLocaleString().

If you want to show the date only in a specific way, you can use one of the many built-in methods. For example, to format the date in a short format:

var today= new Date().toLocaleDateString(); console.log(today); //Ouput: "23/06/2022"12var today= new Date().toLocaleDateString();console.log(today); //Ouput: "23/06/2022"

Sometimes you may need to get the current day, date, month, or year only.

  • Get the current date of the month.
    console.log(today.getDate()); //Ouput: "23"1console.log(today.getDate()); //Ouput: "23"
  • To get day of the week. Where Sunday:0, Monday:1, Tuesday:2, Wednesday:3, Thursday:4, Friday:5 and Saturday:6.
    console.log(today.getDay()); //Ouput: "4"1console.log(today.getDay()); //Ouput: "4"
  • Get the current month of the year.
    console.log(today.getMonth()); //Ouput: "06"1console.log(today.getMonth()); //Ouput: "06"
  • Get the current full year.
    console.log(today.getFullYear()); //Ouput: "2022"1console.log(today.getFullYear()); //Ouput: "2022"
Get Current Time in JavaScript

Use toLocaleTimeString() method to get the time only in a long format:

var today= new Date().toLocaleTimeString(); console.log(today); //Ouput: "11:31:41"12var today= new Date().toLocaleTimeString();console.log(today); //Ouput: "11:31:41"

To format the time in a short format:

var today= new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); console.log(today); //Ouput: "11:31"12var today= new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });console.log(today); //Ouput: "11:31"

In some cases, you may need to get a specific part of the time, like an hour, minute, or second. To get these values see the below example:

var today= new Date(); console.log(today.getTime()); //Ouput: "1655967474165" console.log(today.getHours()); //Ouput: "12" console.log(today.getMinutes()); //Ouput: "28" console.log(today.getSeconds()); //Ouput: "57" console.log(today.getMilliseconds()); //Ouput: "515"1234567var today= new Date(); console.log(today.getTime()); //Ouput: "1655967474165"console.log(today.getHours()); //Ouput: "12"console.log(today.getMinutes()); //Ouput: "28"console.log(today.getSeconds()); //Ouput: "57"console.log(today.getMilliseconds()); //Ouput: "515"

Wrap Up

As you can see, there are many different ways to format the date and time in JavaScript. Just use the built-in Date object and its methods to do so.

Ref From: tecadmin

Related articles