Home > Javascript > Display Time using Javascript

Display Time using Javascript


Here is the quick code to get the current time in 12 hours format using JavaScript.

 <script language="javascript">  
function getTime()
{  
     var dTime = new Date();  
     var hours = dTime.getHours();  
     var minute = dTime.getMinutes();  
     var period = "AM";  
     if (hours > 12)
     {  
         period = "PM" 
     } 
    else { 
         period = "AM"; 
     } 
     hours = ((hours > 12) ? hours - 12 : hours) 
     return hours + ":" + minute + " " + period 
 } 
 </script>

Hope this helps

Good Luck

Advertisements
Categories: Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: