Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
1741
JavaScript Date Object Functions
Introduction
The Date object contains 17 functions. In this section you
will see these 17 functions in some small examples. Many developers used a
combination of these examples to build a clock. There are tons of JavaScript
clocks on the net and some of them look really good. Please take a look at the
date object, if you need more information about that. All functions are used
with the date object. The following examples demonstrate this.
This function can be used to return the current date. It
returns a value between 0 and 31 depending on the month.
<html>
<head>
<script type="text/javascript">
var dt =
new Date()
var str =
dt.getDate()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
14
JavaScript getHours Function
The getHours function can be used to return the current
hour. It will return a value between 0 and 23.
<html>
<head>
<script type="text/javascript">
var dt =
new Date()
var str =
dt.getHours()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
22
JavaScript getMinutes Function
This function can be used to return the current minutes. The
return value is between 0 and 59.
<html>
<head>
<script type="text/javascript">
var dt =
new Date()
var str =
dt.getMinutes()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
17
JavaScript getMonth Function
The getMonth can be used to return the current month. The
return value is between 0 and 11.
<html>
<head>
<script type="text/javascript">
var dt =
new Date()
var str =
dt.getMonth()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
8
JavaScript getSeconds Function
This function can be used to return the current seconds. The
return value is between 0 and 59. If you press the F5 button after few seconds,
you will see that how the seconds increase.
<html>
<head>
<script type="text/javascript">
var dt =
new Date()
var str =
dt.getSeconds()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
19
JavaScript getTimezoneOffset Function
This function can be used to return the minutes between your
time and GMT.
<html>
<head>
<script type="text/javascript">
var dt =
new Date()
var str =
dt.getTimezoneOffset()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
360
JavaScript getYear Function
The getYear function can be used to return the current year.
It will return the year in the format YYYY.
<html>
<head>
<script type="text/javascript">
var dt =
new Date()
var str =
dt.getYear()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
2004
JavaScript setDate Function
This function can be used to set the date of the month. The
value must be between 1 and 31 depending on the current month.
document.write("New
Second is:" + dt.getSeconds())
</script>
</head>
<body></body>
</html>
Output
Current Second:0
setting new second...
New Second is:10
JavaScript setTime Function
The setTime function can be used to set the date and time.
The value is an integer which represents the number of seconds since midnight
January 1, 1970 GMT.
document.write("Converting
to GMT string...<br/>")
var str =
dt.toGMTString()
document.write("GMT
String is:" + str)
</script>
</head>
<body></body>
</html>
Output
Current Date/Time:Thu Sep 16 22:58:20 CST 2004
Converting to GMT string...
GMT String is:Fri, 17 Sep 2004 04:58:20 UTC
JavaScript toLocaleString Function
This function can be used to convert a date to the local
machine settings. Note that many countries uses different date formats.
<html>
<head>
<script type="text/javascript">
var dt =
new Date()
document.write("Locale
String is :" + dt.toLocaleString())
</script>
</head>
<body></body>
</html>
Output
Locale String is :Thursday, September 16, 2004 11:02:02 PM
JavaScript UTC Function
The UTC function can be used to convert the number of
milliseconds between January 1, 1970 and the date, which you pass to the UTC
function. This function has to be called with the following syntax:
Date.UTC(Year, Month, Date, Hour, Minute, Sec)