views:

184

answers:

4

I need a way to turn my 2 character string dates (i.e. '04/10/2010' & '05/24/2010') into an integers to see if one is greater than the other. If the user enters an end date that is less than the begin date I need to popup an "invalid date range" error.

+5  A: 

From what you posted your real problem is:

I need to see if one date is greater than another in javascript/jquery.

If so all you need to use is the Javascript Date object (how to page here).

You can use it as follows:

var dateTextA = '04/10/2010';
var dateTextB = '05/24/2010';
var dateA = new Date(dateTextA);
var dateB = new Date(dateTextB);
if (dateA < dateB){
    alert("Your date is out of range!");
}

Note: Above code has been tested and works in IE7.

If you really feel you need an integer value, you can use the UTC function to get that.

C. Ross
@C. Ross - It looks like all the UTC functions on that page are converting a date to a string..I want to convert a string to date
sadmicrowave
@sadmicrowave Read the code sample.
C. Ross
@C. Ross - new Date(dateA) works perfectly, thanks for your help
sadmicrowave
A: 

please have a look at the Date object.

greetz
back2dos

back2dos
A: 

If Date won't parse what you are looking for, Datejs provides a lot of syntactic sugar to make your life easier.

To compare two dates, all you need to do is turn your strings into Date objects and then use the greater than, less than, or equality operators. Javascript provides Date comparison natively.

justkt
+1  A: 

The trouble is, Date.parse('04/10/2010') returns a timestamp (integer) for April 10 in the US and 4 October most other places.

It is best to use a less ambiguous format- if you are taking user input give them a calendar or menu or three labeled inputs, and build the date from that.

(3 inputs) new Date(+fullyearinput,monthinput-1,+dateinput)

kennebec