.toLocaleTimeString()
Anonymous contributor
Published May 4, 2024
Contribute to Docs
The .toLocaleTimeString()
date method returns a string representation of a given Date
object according to an event’s locale
and other options.
Syntax
dateObject.toLocaleTimeString([locales [, options]])
locales
: A string representing a BCP 47 language tag, such as ‘en-US’ for US English or an array of BCP 47 language tags.options
: An object containing properties that control how the time string is formatted.
Example 1
In the following example, a Date object representing August 3, 2003, is created, and then formatted it using the .toLocaleTimeString()
method:
const date = new Date('2003-08-03T00:00:00');const timeString = date.toLocaleTimeString();console.log(timeString);
The output of the above code is:
12:00:00 AM
Example 2
In the following example, a Date
object representing a specific date and time is created and then formatted into time strings using both English and Chinese locales:
const randomDate = new Date(1995, 11, 17, 3, 24, 0);const options = {hour12: true,hour: 'numeric',minute: 'numeric',second: 'numeric',};const timeStringEnglish = randomDate.toLocaleTimeString('en-US', options);const timeStringChinese = randomDate.toLocaleTimeString('zh-CN', options);console.log('English (en-US):', timeStringEnglish);console.log('Chinese (zh-CN):', timeStringChinese);
The output of the following code is as follows:
English (en-US): 3:24:00 AMChinese (zh-CN): 上午3:24:00
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours