Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Object i.e vs firefox

 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to validate a date that a user enters against the current date. I have it working in firefox but I get that the date I created is invalid in ie.

Can some one tell me what I am doing wrong? (besides using ie)

The code itself is rather long but I have extracted the sections in question.

//vars for current time

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();

// determine month of end date
first_index = end_date.indexOf("-");
last_index = end_date.lastIndexOf("-");
var end_month = end_date.substring( first_index + 1, last_index );

//figure_month_number returns the correct number for each month
var end_month_number = figure_month_number(end_month); //***//
var end_day = end_date.substring(0, first_index); //***//
var end_year = end_date.substring( last_index + 1 ); //***//

//check dates here
if ( new Date( end_month_number+", " + end_day + ", " + end_year ) >
new Date(month +", " + day + ", " + year ) ){
alert("End Date can not be in the future");
return false;
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64826
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and many people that might be able to help you will just move along. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64826
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point do things go awry? Have you checked the results of each line to see when it's going wrong? Just showing us a block of code and saying "it doesn't work" doesn't help us help you.

At what line do the Firefox and IE results differ?
 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies to each of you-sorry.

In firefox, if I put in a future date then that if condition checks it and tell me that I have entered a future date.

In ie. It basically falls through the if but then I have another pop up that shows me what values were entered and it outputs Current Date =NaN

here is that section of code...

var first_date = new Date(start_month_number + start_day + ", "+start_year);
var last_date = new Date(month +", " + day + ", " + year );
alert("start date =" + first_date + "\nCurrent Date =" + last_date);

so in ff I get 2 dates in the alert. In ie I get the first date that I entered and NaN for the value last_date
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I wouldn't use the Date constructor that uses a string which has to be in a certain format. I would use the Date constructor where you pass the year, month, day, and so on as numbers. Less chance for running into issues where standard date formats vary from one place to the next.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64826
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No apologies necessary, just follow the simple debugging techniques I outlined. Just looking at the result and wondering what went wrong isn't instructive.

For example, when you reach this line:
do the variables contain the values you expected? If not, backtrack to find out why.
[ May 06, 2008: Message edited by: Bear Bibeault ]
 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK-that is actually getting me closer. When I construct the end date I take something in the form of 06-may-2008 but the date I get from the clients browser is coming in as 5-6-2008...

and thanks to your advice I found the problem. I didn't need to re-construct a date I just need to compare end date > new Date();

Thanks!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic