This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Date Object i.e vs firefox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Date Object i.e vs firefox" Watch "Date Object i.e vs firefox" New topic
Author

Date Object i.e vs firefox

Pat Peg
Ranch Hand

Joined: Feb 04, 2005
Posts: 194
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

Joined: Jan 10, 2002
Posts: 61103
    
  66

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

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

Joined: Feb 04, 2005
Posts: 194
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 10, 2002
Posts: 61103
    
  66

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

Joined: Feb 04, 2005
Posts: 194
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!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Date Object i.e vs firefox