Solved

JavaScript output the wrong date

Posted on 2011-02-18
5
372 Views
Last Modified: 2012-05-11
Hi all

I have this script below..
when I run the script:  alert(date1);
is outputing the wrong date.. Plus 1 month in the future? how is this possible?

function CompareDates()
{
    var strVon                   = document.getElementById("Fromdate").value;
    var strBis                   = document.getElementById("Todate").value;      
      var currentDate            = new Date();  
      var dt1                    = parseInt(strVon.substring(0,2),10);
    var mon1                   = parseInt(strVon.substring(3,5),10);
    var yr1                    = parseInt(strVon.substring(6,10),10);       
    var dt2                    = parseInt(strBis.substring(0,2),10);
    var mon2                   = parseInt(strBis.substring(3,5),10);
    var yr2                    = parseInt(strBis.substring(6,10),10);
    var date1                   = new Date(yr1, mon1, dt1);
    var date2                   = new Date(yr2, mon2, dt2);

   
      alert(date1);
      alert(mon1);
      alert(currentDate);
      alert(strVon);
      alert(date1);
      
      if(currentDate > date1)
    {
        alert("To date cannot be greater than from date");
        return false;
    }
    else
    {
        alert("Submitting ...");
        document.form1.submit();
    }
}
0
Comment
Question by:ZURINET
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34926306
month start with zero :
january  = 0
december = 11
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34926326
0
 
LVL 29

Expert Comment

by:Badotz
ID: 34926334
The month is zero-based:

var curDate = new Date();
var mon1 = curDate.getMonth(); // 0 - Jan, 1 = Feb, 2 = Mar, etc.

Visit http://www.w3schools.com/jsref/jsref_obj_date.asp for a reference to the Date object.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 34926339
leakim971 is, of course, correct.

Here is some additional information for you: http://www.w3schools.com/jsref/jsref_obj_date.asp
and this article may be helpful: http://www.experts-exchange.com/A_484.html
0
 
LVL 29

Expert Comment

by:Badotz
ID: 34926345
sheesh - great minds think, eh? Mine is just too slow this morning ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to hide html tags in text area 4 32
jQuery detect if it is a mobile device 3 98
Javascript Hashmap output 8 45
Creating a dynamic URL - set dates to upcoming weekend 6 30
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now