Code.GeekInterview.com
 
Code Samples Java Script
 

Dynamic Date and Time


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Beginner

Published: 23rd Apr 2009   Read: 4563 times  

Filed in: Java Script
Add Comment


 

 

Sponsored Links


 

 

Java script for adding dynamic date and time in the webpage

 


Sample Code
  1. <div class='widget-content'>
  2.  
  3. <script language="JavaScript">
  4. <!--
  5.  
  6. function startclock()
  7. {
  8. var thetime=new Date();
  9.  
  10. var nhours=thetime.getHours();
  11. var nmins=thetime.getMinutes();
  12. var nsecn=thetime.getSeconds();
  13. var nday=thetime.getDay();
  14. var nmonth=thetime.getMonth();
  15. var ntoday=thetime.getDate();
  16. var nyear=thetime.getYear();
  17. var AorP=" ";
  18.  
  19. if (nhours>=12)
  20.     AorP="P.M.";
  21. else
  22.     AorP="A.M.";
  23.  
  24. if (nhours>=13)
  25.     nhours-=12;
  26.  
  27. if (nhours==0)
  28.    nhours=12;
  29.  
  30. if (nsecn<10)
  31.  nsecn="0"+nsecn;
  32.  
  33. if (nmins<10)
  34.  nmins="0"+nmins;
  35.  
  36. if (nday==0)
  37.   nday="Sunday";
  38. if (nday==1)
  39.   nday="Monday";
  40. if (nday==2)
  41.   nday="Tuesday";
  42. if (nday==3)
  43.   nday="Wednesday";
  44. if (nday==4)
  45.   nday="Thursday";
  46. if (nday==5)
  47.   nday="Friday";
  48. if (nday==6)
  49.   nday="Saturday";
  50.  
  51. nmonth+=1;
  52.  
  53. if (nyear<=99)
  54.   nyear= "19"+nyear;
  55.  
  56. if ((nyear>99) && (nyear<2000))
  57.  nyear+=1900;
  58.  
  59. document.clockform.clockspot.value=nday+", "+nmonth+"/"+ntoday+"/"+nyear+" "+nhours+": "+nmins+": "+nsecn+" "+AorP;
  60.  
  61. setTimeout('startclock()',1000);
  62.  
  63. }
  64.  
  65. //-->
  66. </script>
  67.  
  68.  
  69.  
  70.  
  71. <form name="clockform">
  72.         <input name="clockspot" size="30" type="text"/>
  73.  
  74. </form>
  75. <script language="JavaScript">
  76. <!--
  77. startclock();
  78. //-->
  79. </script>
  80.  
  81. </div>
  82.  
Copyright GeekInterview.com


Next Article: FormFaces JavaScript


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora17884
2. Durga Prasad12330
3. chowsys6214
4. iamdvr4265
5. Kalidasan3343

Active Coders

# Coder NameCodes
1. srinivasaraobora3
2. chowsys2
3. Durga Prasad1
4. iamdvr1
5. Kalidasan1

Refined Tags