Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JavaScript output the wrong date

Posted on 2011-02-18
5
Medium Priority
?
388 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 83

Accepted Solution

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Suggested Courses

824 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