How To Get Date And Time In User’s Locale Format In JavaScript

Get Date and Time in User’s locale format in JavaScript

If you want to find a method to get Date and Time in User’s locale format in JavaScript, follow this article, and I will help you. Through the functions included in the Date class, you will be able to perform those operations completely. Let’s get started.

Get Date and Time in User’s locale format in JavaScript

Using Date() constructor

Firstly, we will use the Date() constructor to create an object in JavaScript.

let date = new Date();

Next, we will use getDate(), getMonth(), getgetFullYear() to get the day, month, year by user’s locale format.

date.getDate();
date.getMonth();
date.getFullYear();

The first step, we use the new Date() constructor to create the date variable. After that, we call the getDate(), getMonth(), getFullYear() methods and get them into the currentDay, currentMonth and currentYear variables. Finally, we print it to the console. See the code example under.

Complete code:

let date = new Date();
let currentDay = date.getDate();
let currentMonth = date.getMonth() + 1;
let currentYear = date.getFullYear();
console.log(currentYear + "/" + currentMonth + "/" + currentDay);

Output:

2022/10/7

Similarly if you want to get the current time format. You can use the getHours(), getMinutes(), and getSeconds() methods and get them into hours, minutes, seconds variables. Following the code example below.

Example:

let date = new Date();
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
console.log(hours + ":" + minutes + ":" + seconds);

Output:

20:47:43

So now we will combine the Date and Time and we have Date and Time in User’s locale format in JavaScript.

Example:

let now = new Date();

// Get Date
let currentDate = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();

// Get Time
let currentTime = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();

// Merge Date and Time, and print
let dateTime = currentDate + ' ' + currentTime;
console.log(dateTime);

Output:

2022-10-7 20:57:24

Besides, you can use the Date() constructor to get current Date and Time.

Code example:

let date = new Date();
console.log(date);

Output:

Fri Oct 07 2022 21:09:03 GMT+0700 (Indochina Time)

Using toLocaleString() method

Here, we will use the toLocaleString() method to get Date and Time and return a new string is in User’s locale format.

Syntax:

toLocaleString()

Parameters: None

Return: Current date in String.

Example:

let date = new Date();
console.log(date.toLocaleString());

Output:

10/7/2022, 9:12:43 PM

If you want to get single date or time , you can use toLocaleDateString() to get Date or use toLocaleTimeString() to get Time. See code below.

Example:

let date = new Date();
console.log(date.toLocaleDateString());
console.log(date.toLocaleTimeString());

Output:

10/7/2022
9:15:02 PM

Summary

There are many ways to get Date and Time in User’s locale format in JavaScript. However, the two methods mentioned in this article are the most straightforward approaches. Hopefully, they will help you. Leave a comment below if you have any questions. Have a good day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *