JavaScript Date.toLocaleTimeString() Method
-
Syntax of JavaScript
date.toLocaleTimeString()
Method -
Example Codes: Use the
date.toLocaleTimeString()
Method -
Example Codes: Use the
date.toLocaleTimeString()
Method With the Optional Parameterlocales
-
Example Codes: Use the
date.toLocaleTimeString()
Method With the Optional Parameteroptions
In JavaScript, the Date
object is used to represent the Date and Time. We can declare this object with the new
keyword.
This object provides different methods to customize the date and time. One of them is the date.toLocaleTimeString()
method which converts the Date
object to time and represents it as a string.
Syntax of JavaScript date.toLocaleTimeString()
Method
date.toLocaleTimeString(locales, options)
Parameters
locales (optional) |
This refers to the countries’ languages that can be passed as an array of strings; this will format the time in a specific language. By default, this uses the browser’s default language. |
options (optional) |
This refers to an object that customizes the time format with different properties, including dayPeriod , hour , minute , second , and functionalSecondDigit . If these properties are not provided, the hour, minute, and second will be numeric. |
Return
A string value representing the Time portion of the Date
object is returned based on the specific language conventions of the locale.
Example Codes: Use the date.toLocaleTimeString()
Method
const date = new Date()
let timeString = date.toLocaleTimeString()
console.log(timeString)
Output:
9:40:48 PM
The Date
object was declared in the date
variable. We get access to the toLocaleTimeString()
method from the Date
object using a dot (.)
This method returns the time as a string. To store the result, we declare a variable and print the variable to see the result in the output.
Example Codes: Use the date.toLocaleTimeString()
Method With the Optional Parameter locales
const date = new Date();
let USTimeString = date.toLocaleTimeString('en-US')
let KoreanTimeString = date.toLocaleTimeString('ko-KR')
let PersianTimeString = date.toLocaleTimeString('fa-IR')
console.log(USTimeString)
console.log(KoreanTimeString)
console.log(PersianTimeString)
Output:
9:43:25 PM
오후 9:43:25
۲۱:۴۳:۲۵
We provide different countries’ languages as the locales
parameter to format our time string in that specified language. This parameter is optional; if we do not specify any locales
, it will print the time string based on the browser’s selected language.
Example Codes: Use the date.toLocaleTimeString()
Method With the Optional Parameter options
const date = new Date()
const options = { timeZone: 'UTC', hour: '2-digit', minute: '2-digit' , second : '2-digit' };
let timeStringWithOptions = date.toLocaleTimeString('en-US', options)
console.log(timeStringWithOptions)
Output:
03:53:01 PM
We define different options in the options
object, which returns the customized time strings as the output based on our provided options
parameters. If we do not give any options, it will print the time string as numeric.
Niaz is a professional full-stack developer as well as a thinker, problem-solver, and writer. He loves to share his experience with his writings.
LinkedIn