JavaScript Date.toTimeString() Method
-
Syntax of JavaScript
date.toTimeString()
Method -
Example Codes: Use the
date.toTimeString()
Method -
Example Codes: Use the
date.toTimeString()
Method Without Providing Time
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 these is the date.toTimeString()
method which converts the Date
object to time and represents it as a string.
Syntax of JavaScript date.toTimeString()
Method
date.toTimeString()
Parameters
The date.toTimeString()
method does not accept any parameter.
Return
A string value of time is returned from the Date
object.
Example Codes: Use the date.toTimeString()
Method
const date = new Date()
let timeString = date.toTimeString()
console.log(timeString)
Output:
13:53:10 GMT+0600 (Bangladesh Standard Time)
The Date
object has been declared in the date
variable. We used the date.toTimeString()
method to represent time as a string.
This method returns the time as a string in the local time zone. To store the result, we declared a variable and printed the variable to see the result in the output.
Example Codes: Use the date.toTimeString()
Method Without Providing Time
const date = new Date('September 9, 2022')
let timeString = date.toTimeString()
console.log(timeString)
Output:
00:00:00 GMT+0600 (Bangladesh Standard Time)
We provided the date in the Date
constructor and kept the time portion empty. As a result, this method returned the time as 00:00:00
.
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