.toTimeString()

Anonymous contributor's avatar
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 date
const newDate = new Date('March 8, 1990 12:45:00');
// Get the time string from the Date object
const 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 1990
Time 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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy