Development| HTML| JavaScript| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JavaScript setMonth() method 
 

JavaScript setMonth() method is used to set month for the date object.

 

JavaScript setMonth() method

                         

JavaScript setMonth() method is used to set month for the date object.

Syntax:

 dateObject.setMonth( month ,day );

Where month is a number between 0 to 11 and its representations are as follows :

Month Number  Represents Month
0 January
1 February
2 March
3 April
4 May
5 June
6 July
7 August
8 September
9 October
10 November
11 December

The parameter day is optional and consists a numerical value between 1 to 31 that represents the day. Note that we must need to have a date object to call this method.

Description of code :

To illustrate the use of setMonth() method we have created a simple html page and in this page we have provided a text box. User can enter month number here. We also have suggested that user must input the month number between 0 to11. If user enters any thing else than valid month number between 0 to 11 then it shows "Invalid month number" message into alert method.

When user clicks on the button "Set Month" after inserting month it checks that whether month number provided is valid or not. Here is the full example code :

<html>
<head>
<script language="JavaScript">
   function setMonthFunction()
   {
    var dt= new Date();
    var month = document.getElementById('monthValue').value;
    if(month >=0 && month <=11){
       	alert("Current date :" +dt);
	dt.setMonth(month);
	alert("Date after set :"+dt);
	return true;
     }
    else
     {
	alert("Invalid month number");
	return false;
     }
   }
</script>
</head>
<body>
<div style="background: #ff9900; width:'100%';"
      align="center">
  <font color="#0000ff" size="12pt">
	<b>setMonth() Example</b>
  </font>
 </div>
 <center>
    Insert any number between 0 to 11 to set Month
    <input type="text" id="monthValue" /><br>
    <input type="button" value="Set Month" 
     onClick="return setMonthFunction();"/>
 </center>
</body>
</html>

Output :

Input valid month number to set into setMonth() method.

If user enters any thing else than a valid month number then it will show message "Invalid month number" into an alert message. For example if we enter Jan, Feb or any thing other than a number between 0 to 11 then it shows an alert message.

If we enter "12" and click on the button "Set Month",

date before calling setMonth() method will be look like this

Date after calling the method setMonth() would look like as follows :

Download Source Code

                         

» View all related tutorials
Related Tags: java javascript html c table script delete object header method ip page oo create tab footer with e il it

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.