views:

39

answers:

2

I'm trying to sort a list of dates, but I'm struggling with null dates which aren't being handled consistently.

So I need something like:

var date = Date.parse(dateString);
if (!date) {
    date = Date.MinValue;
}

but I'm struggling to find the correct syntax. Thanks


Update: The bug turned out to be a different problem. I have Datejs imported for use in another part of the project, so I hadn't realised that Datejs defines a Date.parse() method which was overriding the standard JavaScript funtion.

Anyway, it turns out that Datejs has a weird bug which means it doesn't handle dates begining with "A" properly. So actually my null dates were being ordered correctly, it was just April and August dates were then being mixed up with them.

And In case anyone with the same problem stumbles accross this post, the fix is to use the Datejs Date.parseExact method which lets you provide a specific format string, see [this link][1].

Thanks for answers anyway :)

[1]: http://Date.parseExact("15 Apr 2009","d MMM yyyy")

+2  A: 

parse() returns NaN on an impossible parse so how about just;

 var date = Date.parse(dateString) || 0;
Alex K.
A: 

You could use isNaN(date) to check for invalid date. Are you sure you want to use "MinValue; from Date? I was running the js on chrome and kept on getting undefined.

Ravindra