We previously left off discussing a smattering of JavaScript Date Objects. In this tutorial we will continue the discussion, picking up with the GetFullYear() Method. There are a ton of Date methods, so let's get started.
Javascript Objects: More Date Methods - The GetHours() Method (Page 2 of 5 )
This method returns the hour as a two digit number (unless of course the time is less than ten, in which case it is a one digit number, duh).
<html>
<body>
<script type="text/javascript">
var hours = new Date()
document.write(hours.getHours())
</script>
</body>
</html>
This program creates a variable named hours and stores the current hour based on your system time in it. It then writes it to the screen, like so:
16
If we wanted to extract the hour from a specified date within a variable, we could do that as well:
<html>
<body>
<script type="text/javascript">
var fluxcapacitor = new Date("April 22, 1977 10:30:00")
document.write(fluxcapacitor.getHours())
</script>
</body>
</html>
This sample creates a variable named fluxcapacitor and stores the value April 22, 1977 10:30:00 in it (the time the great James Payne entered the world muahaha). It then retrieves the hour from the variable and writes it to the screen, resulting in:
10
The GetMinutes() Method
This works like the GetHours() method, only with minutes.
<html>
<body>
<script type="text/javascript">
var minutes = new Date()
document.write(minutes.getMinutes())
</script>
</body>
</html>
The result: 22
The GetSeconds() Method
Again, the code is the same except it is for seconds: