views:

335

answers:

5

I get two strings formated like (Brazilian Format): "DD/MM/YYYY", I need to compare both. Since the first field is the begin and the last is the end,

My validation is begin <= end

Date.new(begin) is generating 'invalid date' even on ISO !

+4  A: 

Don't use Date.new. Use new Date(). Because of the format of your date string, I would recommend grabbing each field individually and passing them into the constructor:

var startYear = parseInt(document.getElementById('startYear'), 10);
var startMonth = parseInt(document.getElementById('startMonth'), 10) - 1; // as per Residuum's comment
var startDay = parseInt(document.getElementById('startDay'), 10);
var start = new Date(startYear, startMonth, startDay);

etc. If you're handed a date string, then you can use fuzzy lollipop's method to get each field from the string. I'm not sure if the Date constructor will accept unparsed strings for the individual fields, however.

The, once you have the two dates you'd like to compare, just compare their values in milliseconds since the epoch:

function isValid(start, end) {
    return start.getTime() < end.getTime();
}
Matt Ball
Months are zero-based in Javascript, so make it startMonth = parseInt(document.getElementById('startMonth'), 10) - 1;
Residuum
Whoops. Thanks!
Matt Ball
Ok, special thanks for the Month - 1 observation.. Otherwise, all december dates would fail =)
Fabiano PS
+1  A: 

you can do this, if you know your date will always be formatted the same way dd/mm/yyyy

today = "23/02/1001";
dateComponents = today.split("/");
date = new Date(dateComponents[2], dateComponents[1] - 1, dateComponents[0]);

but a better solutions is to look at this page there is Datejs which is a good alternative to date processing.

fuzzy lollipop
Months are zero-based in Javascript, so make it date = new Date(dateComponents[2], dateComponents[1] - 1, dateComponents[0]);
Residuum
+3  A: 

There's a nice date handling library called datejs which has a parseExact(dateStr, format) method.

Darin Dimitrov
I will check this, tnxs!
Fabiano PS
A: 

Quick 'n dirty :

function is_valid (start , end) {
     return start.split('/').reverse().join('') <= end.split('/').reverse().join('') ;
}

That is, split the date into components, reverse the order join them again and do a string comparison.

Edit: As noted in the comment, of course this won't work if your month/days smaller than 10 are not zero padded.

Pat
The date format string dd/mm/yyyy typically does not assume zero padding.This does not work if the date components are not always zero padded.E.g. '9/1/2010' and '10/1/2010'.
Mike Samuel
if it does not have padding then it won't work ... to me indicating dd instead of d and mm instead of m means 2 digits but I guess that's not necessarily the case.
Pat
A: 

Here are all available constructors for date objects in Javascript:

dateobject = new Date(); // returns date of current time stamp
                         // on the browser
dateobject = new Date("Month Day, Year Hours:Minutes:Seconds");
dateobject = new Date(Year, Month, Day);
dateobject = new Date(Year, Month, Day, Hours, Minutes, Seconds);
dateobject = new Date(Milliseconds);

Pick the one you try to use, but I would go for new Date(Year, Month, Day); in your case.

EDIT: Note: Monthis zero-based in Javascript, so January 1 2010, will be new Date(2010, 0, 1) and December 31 2011 is new Date(2010, 11, 31).

Residuum
This Month observation is very handy, and JS is very stupid
Fabiano PS
The month stuff is a very strange JS quirk, as there are some others, but if you grok JS it can be a really nice language.
Residuum