Find current time without date in JavaScript
This post will discuss how to find the current time without date in JavaScript.
There are several ways to find the current time without the date in JavaScript. A time without the date is a time that only contains the hours, minutes, and seconds, and ignores the year, month, and day. Here are some of the common functions:
1. Using Date.prototype.toLocaleTimeString() function
The Date.prototype.toLocaleTimeString() function returns a string with a language-sensitive representation of the time portion of a date object. It takes an optional locale parameter and an optional options parameter that can specify the hour, minute, second, and time zone format. This function works well for displaying the current time in a human-readable way. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// create a Date object with the current date and time const now = new Date(); // sample output: 4:39:09 PM console.log(now.toLocaleTimeString()); // sample output: 4:39:09 PM console.log(now.toLocaleTimeString('en-GB')); // sample output: 16:39:09 console.log(now.toLocaleTimeString('en-US', {hour12: false})); // sample output: 10:09:09 PM console.log(now.toLocaleTimeString('en-US', {timeZone: 'Asia/Kolkata'})); // sample output: 04:39:09 PM console.log(now.toLocaleTimeString('en-US', {hour: '2-digit', minute: '2-digit', second: '2-digit'})); |
2. Using Date.prototype.toTimeString() function
We can use the Date.prototype.toTimeString() function to get the current time as a human-readable string in the default time zone. This function returns a string with the time portion of a date object, followed by the time zone offset in parentheses. It does not take any parameters and it always uses 24-hour format. We can further use the slice() function to extract only the time part of the returned string, which will be in HH:MM:SS format. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// get current date and time const now = new Date(); // get current time const time = now.toTimeString(); // sample output: 16:39:09 GMT+0000 (Coordinated Universal Time) console.log(time); // get the first 8 characters of the time string const timeString = time.slice(0, 8); // prints something like "16:39:09" console.log(timeString); |
3. Using Date.prototype.get*() functions
We can use Date.prototype.getHours(), Date.prototype.getMinutes(), and Date.prototype.getSeconds() built-in functions that return the hour, minute, and second components of a date object, respectively. They do not take any parameters and they always use 24-hour format. We can use these functions to get the individual components of the time and format them as we like. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// get the current date and time const now = new Date(); // get the hour component (0-23) const hours = now.getHours(); // get the minute component (0-59) const minutes = now.getMinutes(); // get the second component (0-59) const seconds = now.getSeconds(); // format the time as a string const time = `${hours}:${minutes}:${seconds}`; // prints something like "16:39:09" console.log(time); |
That’s all about finding the current time without date in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)