In this tutorial we are going to learn format dates and times in JavaScript. This is require part of developing custom web app and dynamic data building. toLocalString() method is powerful and flexible for convert timestamp in user local time or customize date format. it also support multiple regions timezone.
We will covered depth tutorial of, how to format dates and times in javascript using toLocalString(), locale timezone options, UTC timestamp to readable local dates using javascript. Also cover toLocalString() options for date and time.
What is toLocaleString() in JavaScript?
toLocaleString() is JavaScript method which convert JavaScript date object into human-readable format. it can provide customized parameter so, easily formatted date with parameter.
dateObject.toLocaleString(locale, options);
- locale(optional) : Specify language and regions (eg. en-US for US English).
- options(optional) : Customized date and time formats.
Note : If no local is specify then method use system default.
Creating a JavaScript Date Object
Require initiate Date object class.
// Create a date object for the current date and time const currentDate = new Date(); //Sun Mar 23 2025 22:48:37 GMT+0530 (India Standard Time) console.log(currentDate.toString());
Formatting Dates and Times Using toLocaleString()
toLocaleString() Coverts date and time to local timezone.
const localDate = new Date(); console.log(localDate.toLocaleString());
Formatting Dates for Different Locales
Formatting date and time for specific locales, pass locale string as argument. it can support multiple locales, few of them are shown below:
const localDate = new Date(); //US English locales. console.log(localDate.toLocaleString('en-US')); //French Locales. console.log(localDate.toLocaleString('fr-FR')); //Japanese Locale console.log(localDate.toLocaleString('ja-JP'));
Customizing Date and Time Format Using toLocaleString() Options
The options parameter helps for customize the output format. Here’s a list of used options:
year: 'numeric' | '2-digit' month: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow' day: 'numeric' | '2-digit' hour: 'numeric' | '2-digit' minute: 'numeric' | '2-digit' second: 'numeric' | '2-digit' timeZoneName: 'short' | 'long'
1. Custom Date and Time Formatting in JavaScript
const options = { year: 'numeric', month: 'long', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short' }; console.log(localDate.toLocaleString('en-US', options)); // Output: "March 23, 2025, 06:30:00 AM GMT"
If you are using JavaScript toLocaleString() options for date and time, this will give you full control of format displaying.
2. Formatting Dates for Different Time Zones
To display the date and time in different time zones using javascript, use the timeZoneOptions attribute. Here is example.
const timeZoneOptions = { timeZone: 'America/New_York', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short' }; console.log(localDate.toLocaleString('en-US', timeZoneOptions)); //March 23, 2025 at 11:17:45 PM GMT+5:30
Convert UTC to Local time using toLocalString()
function convertUTCToLocal(utcDateString, locale = 'en-US') { const utcDate = new Date(utcDateString); return utcDate.toLocaleString(locale); } console.log(convertUTCToLocal('2025-03-23T10:30:00Z'));
If you are looking for How to convert a UTC timestamp to readable local date in JavaScript, toLocalString() can help with that.
Conclusion
toLocalString() function provide all formatting needs of date and time. this will helps for converting UTC time zone to local timezone and options for date and time you can customized date format.