JavaScript Date Object Time functions

Updated on 21 Mar 2012,
Published on 20 Mar 2008

JavaScript Date Object also provides the functions to get the parts of current time. To get the Time parts such as hours, minutes, seconds and milliseconds you can define a variable for Date Object using new keyword. This will provide all the methods for retrieving the parts of the Time as well as Date, from which you can use Time related functions to work upon.

JavaScript Data Object Examples:

You can see the live samples and examples of JavaScript Data Object from the following links:

JavaScript Date Object Methods to retrieve the Time Parts

getHours()
getHours function returns the current Hour of Time retrieved using Date Object range from 0 to 23.

getMinutes()
getMinutes function returns the current minute of Time retrieved using Date Object range from 0 to 59.

getSeconds()
getSeconds function returns the current second of Time retrieved using Date Object range from 0 to 59.

getMilliseconds()
getMilliseconds function returns the milli seconds of Time retrieved using Date Object range from 0-999.

getTimezoneOffset()
getTimezoneOffset returns the difference between current local time and Greenwich Mean Time in minutes.

JavaScript code Example to retrieve Time Parts using Date Object

// Date object defined with new keyword.
var myDate = new Date();

// JavaScript getHours Function
document.write(myDate.getHours());

// JavaScript getMinutes Function
document.write(myDate.getMinutes());

// JavaScript getSeconds Function
document.write(myDate.getSeconds());

// JavaScript getMilliseconds Function
document.write(myDate.getMilliseconds());

// JavaScript getTimezoneOffset Function
document.write(myDate.getTimezoneOffset());

You can divide the getTimezoneOffset with 60 to get the GMT difference in hours. It will return the hours in +/- according to your country.

Output:

You can see the output of above discussed code from the following link:

Time Functions

Continue to next tutorial: Javascript Convert String to Date to learn how to convert the string type value having valid date format into  date type and apply JavaScript Date Object functions over it.

0 Responses to "JavaScript Date Object Time functions"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers