Below is the example of Date toLocaleTimeString() method.
// Here a date has been assigned
// while creating Date object
'July 16, 2018 05:35:32'
// time from above date object is
// being extracted using toLocaleTimeString()
B = dateobj.toLocaleTimeString();
// Printing time
The date.toLocaleTimeString() method is used to fetch the time from a given Date object.
Parameter: This method does not accept any parameter. It is just used along with a Date Object from which we want to fetch the time.
Return Values: It returns a string which is time from given Date object.
Note: The DateObj is a valid Date object created using Date() constructor from which we want to fetch the time.
More codes for the above method are as follows:
PRogram 1: The date of the month should lie in between 1 to 31 because none of the months have the date greater than 31 that is why it returns Invalid Date because the date for the month does not exist. Hence, Year will not have existed when the date of the month is given as 36 i.e, greater than 31.
Program 2: If nothing as parameter is given to the Date() constructor the current date is passed in Date object and hence toLocaleTimeString() will return current time.
Output: The time returned in this program is Current time
- Google Chrome
- Internet Explorer
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.