Jquery DateJs, is there validation for full date?
Asked Answered
G

2

1

I just find out about the power of date js, And its great!!! As I am a newbie I was wondering if there is any kind of general validitation for different types of full dates. eg.

var d1 = Date.parse('2000-10-18, 10:06 AM');
alert(d1.toString('HH:mm'));

If date is ('200-10-18, 10:06 AM'), of course it doesn't like it.

So my question is if there is any quick way to validate the full date, rather than having to validate one by one.

Thanks.

Galaxy answered 9/3, 2010 at 10:17 Comment(0)
B
6

Here I am answering your question again.

To validate a date, you can simply call:

var d1 = "200-10-18, 10:06 AM";
Date.validateDay(d1); 

And it will tell you if the date is valid.

EDIT

Or, you can simply do this:

var d = Date.parseExact("2008-03-08", "yyyy-MM-dd"); 
if (d !== null) { 
     alert('Date = ' + d.toString('MMMM dd, yyyy')); 
} 

Using parseExact will return null in case the date is not valid.

Basiliabasilian answered 9/3, 2010 at 10:26 Comment(3)
What exactly is a "valid date"? One that returns a number from parse?Cominform
One that doesn't return null when using parseExact (i.e. matches your criteria and passes datejs validation)Basiliabasilian
This isn't working, the valid parameterS for validateDay are numbers: Date.validateDay ( Number day, Number fullYear, Number monthNumber ) : Boolean see code.google.com/p/datejs/wiki/APIDocumentation I wonder how this answer was marked as correct.Shadbush
C
0

Date.parse will parse date and time together if that's what you mean. But it doesn't like AM/PM, so you would need to convert your hour to 24 hour time.

I suggest, if you know it ends in AM or PM, just stripping that off, and if it's AM, you're fine. If it's PM, add 60 * 60 * 12 * 1000 back to the result to get your 12 hours back. (seconds * minutes * hours * milliseconds).

Try again with: var d1 = Date.parse('2000-10-18, 10:06');

Also, if you want to be sure the date is valid, parse will return NaN (not a number) if the input is invalid. But you can pretty much enter any date/time within a few thousand years, I believe. It will only start throwing NaN when you try to either parse a date with some screwed up syntax (like using a dash for one part and a slash for another) or if you try Date.parse("bananasandwitch").

Cominform answered 9/3, 2010 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.