Solved

JavaScript output the wrong date

Posted on 2011-02-18
5
370 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
Comment Utility
month start with zero :
january  = 0
december = 11
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
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
Comment Utility
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
Comment Utility
sheesh - great minds think, eh? Mine is just too slow this morning ;-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now