This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
JavaScript
JavaScript Date Object
JavaScript Loops
JavaScript Operators
JavaScript Conditional Statements
JavaScript Boolean Object
JavaScript Assertiveness
JavaScript Array Object Functions
JavaScript Arrays
JavaScript Variables
How to use JavaScript?
JavaScript Guidelines
Stock sorter example for IE5
Confused about how to use a data island in IE5??
Software Release tracking application
Employee Phonebook dataisland
Multitab DHTML browser
Trim function in Javascript
Multiple Grid
Re: Trim Function in JavaScript
Re: Trim Function in JavaScript
 
LAMP >>

By :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 :8259

 

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.

JavaScript getDate Function
JavaScript getHours Function
JavaScript getMinutes Function
JavaScript getMonth Function
JavaScript getSeconds Function
JavaScript getTimezoneOffset Function
JavaScript getYear Function
JavaScript setDate Function
JavaScript setHours Function
JavaScript setMinutes Function
JavaScript setMonth Function
JavaScript setSeconds Function
JavaScript setTime Function
JavaScript setYear Function
JavaScript toGMTString Function
JavaScript toLocaleString Function
JavaScript UTC Function

JavaScript getDate Function

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.

<html>

<head>

<script type="text/javascript">

            var dt = new Date()

            document.write("Current Date:" + dt.getDate() + "<br/>")

            document.write("setting new date...<br/>")

            var str = dt.setDate(10)

            document.write("New Date is:" + dt.getDate())

</script>

</head>

<body></body>

</html>

Output

Current Date:16
setting new date...
New Date is:10

JavaScript setHours Function

This function can be used to set the hours. The value must be between 0 and 23.

<html>

<head>

<script type="text/javascript">

            var dt = new Date()

            document.write("Current Hour:" + dt.getHours() + "<br/>")

            document.write("setting new hour...<br/>")

            var str = dt.setHours(10)

            document.write("New Hour is:" + dt.getHours())

</script>

</head>

<body></body>

</html>

Output

Current Hour:21
setting new hour...
New Hour is:10

JavaScript setMinutes Function

The setMinutes function can be used to set the minute. The value must be between 0 and 59.

<html>

<head>

<script type="text/javascript">

            var dt = new Date()

            document.write("Current Minute:" + dt.getMinutes() + "<br/>")

            document.write("setting new minutes...<br/>")

            var str = dt.setMinutes(10)

            document.write("New Minute is:" + dt.getMinutes())

</script>

</head>

<body></body>

</html>

Output

Current Minute:38
setting new minutes...
New Minute is:10

JavaScript setMonth Function

The setMonth function can be used to set the current month. The value must be between 0 and 11, where 0 is January and 11 is December.

<html>

<head>

<script type="text/javascript">

            var dt = new Date()

            var month = dt.getMonth() + 1

            document.write("Current Month:" + month + "<br/>")

            document.write("setting new month...<br/>")

            var str = dt.setMonth(01)

            document.write("New Month is:" + dt.getMonth())

</script>

</head>

<body></body>

</html>

Output

Current Month:9
setting new month...
New Month is:1

JavaScript setSeconds Function

The setSeconds can be used to set the current seconds. The value must be between 0 and 59.

<html>

<head>

<script type="text/javascript">

            var dt = new Date()

            document.write("Current Second:" + dt.getSeconds() + "<br/>")

            document.write("setting new second...<br/>")

            var str = dt.setSeconds(10)

            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.

<html>

<head>

<script type="text/javascript">

            var dt = new Date()

            document.write("Current Time:" + dt.getTime() + "<br/>")

            document.write("setting new time...<br/>")

            var str = dt.setTime(10)

            document.write("New Time is:" + dt.getTime())

</script>

</head>

<body></body>

</html>

Output

Current Time:1095392980609
setting new time...
New Time is:10

JavaScript setYear Function

The setYear function is used to set the current year.

<html>

<head>

<script type="text/javascript">

            var dt = new Date()

            document.write("Current Time:" + dt.getYear() + "<br/>")

            document.write("setting new year...<br/>")

            var str = dt.setYear(2090)

            document.write("New Year is:" + dt.getYear())

</script>

</head>

<body></body>

</html>

Output

Current Time:2004
setting new year...
New Year is:2090

JavaScript toGMTString Function

This function can be used to convert the date to a string using the GMT convention.

<html>

<head>

<script type="text/javascript">

            var dt = new Date()

            document.write("Current Date/Time:" + dt + "<br/>")

            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)

<html>

<head>

<script type="text/javascript">

            document.write("UTC :" + Date.UTC(2004, 08, 21, 08, 09, 44))

</script>

</head>

<body></body>

</html>

Output

UTC :1095754184000


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP