Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JavaScript output the wrong date

Posted on 2011-02-18
5
Medium Priority
?
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

670 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