Core JavaScript

Intermediate JavaScript

JavaScript Reference

JavaScript Date and Time Functions




JavaScript is use to set the current date/time in different different format (like milliSecond, minutes, hours etc).


Displaying the Current Date

Example-Code:

<script type="text/javascript"> 
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth()
var year = currentDate.getFullYear()
document.write("Current Date: ");
document.write("<strong>"+ day +"-"+ month +"-"+ year +"</strong>");
</script>
Online Try This Code Your Self....   »

Example-Result:


Displaying the Current Time 24 Hours

Example-Code:

<script type="text/javascript"> 
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

if (minutes < 10)
 { 
   minutes = "0" + minutes;
 }
 document.write("Current Time: ");
 document.write("<strong>"+hours+":"+minutes+" "+"</strong>");
</script>
Online Try This Code Your Self....   »

Example-Result:


Displaying the Current Time AM/PM format

Example-Code:

<script type="text/javascript"> 
  var currentTime = new Date()
  var hours = currentTime.getHours()
  var minutes = currentTime.getMinutes()
 
  var am_pm = "AM";
  if (hours >= 12) {
  am_pm = "PM";
  hours = hours - 12;
  }
  if (hours == 0) {
  hours = 12;
  }
 
  if (minutes < 10)
  {
   minutes = "0" + minutes;
  }
  document.write("Current Time: ");  
  document.write("<strong>"+hours+":"+minutes+" "+am_pm+"</strong>");
</script>
Online Try This Code Your Self....   »

Example-Result:

JavaScript Date Time Functions


Function Methods                         Event Handlers
Date Function
getDate() Get the Date of the Month.
getDay() Get the Day of the Week.
getFullYear() Get the Full Year in four Digit.
getHours() Get the Hours between 0 to 23.
getMilliseconds() Get the Milliseconds between 0 to 999.
getMinutes() Get the Minutes between 0 to 59.
getMonth() Get the Month between 0 to 11.
getSeconds() Get the Seconds between 0 to 59.
getTime() Get the Milliseconds since Jan 1, 1970.


getUTCDate() Get the Universal Time Date of the Month.
getUTCFullYear() Get the Universal Time Full Year in four Digits.
getUTCHours() Get the Universal Time Hours beetween 0 to 23.
getUTCMilliseconds() Get the Universal Time Milliseconds (0 to 999).
getUTCMinutes() Get the Universal Time Minutes between 0 to 59.
getUTCMonth() Get the Universal Time Month between 0 to 11.
getUTCSeconds() Get the Universal Time Seconds between 0 to 59.


setDate() Set the Date of the Month.
setFullYear() Set the Full Year in four Digit.
setHours() Set the Hours beetween 0 to 23.
setMilliseconds() Set the Milliseconds between 0 to 999.
setMinutes() Set the Minutes between 0 to 59.
setMonth() Set the Month between 0 to 11.
setSeconds() Set the Seconds between 0 to 59.
setTime() Set the Milliseconds since Jan 1, 1970.


setUTCDate() Set the Universal Time Date of the Month.
setUTCFullYear() Set the Universal Time Full Year in four Digits.
setUTCHours() Set the Universal Time Hours between 0 to 23.
setUTCMilliseconds() Set the Universal Time Milliseconds (0 to 999).
setUTCMinutes() Set the Universal Time Minutes between 0 to 59.
setUTCMonth() Set the Universal Time Month between 0 to 11.
setUTCSeconds() Set the Universal Time Seconds between 0 to 59.


toString() Converts the Date object into string.
toUTCString() Converts the Date object into Universal Time string.