Home / Web Development / HTML / How to get current Time using JavaScript

How to get current Time using JavaScript

Hi Guys,
Today we discuss one useful topic related to javascript like How to get current time or live time using javascript. Here I explain how to display Current Time in 12 hour format (AM / PM) and 24 hour format with Hours Minutes, Seconds (hh:mm:ss) using JavaScript. The 12 hour time will be displayed in hh:mm:ss tt format with AM / PM and the 24 hour time will be displayed in hh:mm:ss format using JavaScript.

From the above code, we get the output like “21:01:26”. First we store the todays date to a variable in javascript using new Date(). The getHours() method is helps to get the hour for a given date, according to local time. The value returned by getHours() is an integer between 0 and 23. The getMinutes() method helps to get the minutes in the specified date according to local time. The value returned by getMinutes() is an integer between 0 and 59. The getSeconds() method helps to get the seconds in the specified date according to local time. The value returned by getSeconds() is an integer between 0 and 59.
Now we got the current time. But here is one problem, basically this code is correct. But I modified the code like AM and PM based. AM and PM : AM stands for ‘ante meridiem’, which means ‘before noon’ in Latin, while PM stands for ‘post meridiem’, which means ‘after noon’ in Latin. This is very simple method in javascript.
h >= 12 ? ‘PM’ : ‘AM’;

Full code

The output like:Current Time: 9:24:04 PM

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media Auto Publish Powered By : XYZScripts.com