Solved

JavaScript output the wrong date

Posted on 2011-02-18
5
380 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javascript and jquery question 4 46
Prevent certain words from being typed in a form 6 55
Open grouped links on a page in new tabs via jquery 5 61
javascript delete path 7 44
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

734 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