Wednesday 25 November 2009

A Simple Javascript to display date and time


Displaying the current Date:
 

var currentDate = new Date()
  var day = currentDate.getDate()
  var month = currentDate.getMonth()
  var year = currentDate.getFullYear()
  document.write("<b>"   day   "/"   month   "/"   year   "</b>")      


 Displaying the current time :

 var currentTime = new Date()
  var hours = currentTime.getHours()
  var minutes = currentTime.getMinutes()

  if (minutes < 10)
  minutes = "0"   minutes

  document.write("<b>"   hours   ":"   minutes   " "   "</b>")


Displaying the current time with AM/PM


var currentTime = new Date()
  var hours = currentTime.getHours()
  var minutes = currentTime.getMinutes()

  var suffix = "AM";
  if (hours >= 12) {
  suffix = "PM";
  hours = hours - 12;
  }
  if (hours == 0) {
  hours = 12;
  }

  if (minutes < 10)
  minutes = "0"   minutes

  document.write("<b>"   hours   ":"   minutes   " "   suffix   "</b>")


These Scripts can be implemented easily by  calling from external javascript.

Copy any of the three code and paste in notepad and save as time.js
Example-<script src="http://sites.google.com/site/ishanbanga03/my-uploads/timeampm.js"></script>

No comments:

Post a Comment