JavaScript .toTimeString()
Anonymous contributor
Published Jan 17, 2024
Contribute to Docs
The .toTimeString() method returns a string representing the time portion of the given Date object in the local timezone.
Syntax
DateObject.toTimeString()
Example
The following code creates a Date object called newDate and calls the .toTimeString() method to obtain the time portion from the newDate object:
// A Date object with a random dateconst newDate = new Date('March 8, 1990 12:45:00');// Get the time string from the Date objectconst timeString = newDate.toTimeString();console.log('New Date:', newDate.toDateString());console.log('Time String:', timeString);
The above code will result in the following output:
New Date: Thu Mar 08 1990Time String: 12:45:00 GMT+0530 (India Standard Time)
Note: The
.toTimeString()method returns the time according to the local timezone. The output may vary according to the timezone.
Codebyte Example
Following is a runnable codebyte example of .toTimeString() method:
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
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours