Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1349 How can I check that a string contains a date?

irt.org | Knowledge Base | JavaScript | Date | Q1349 [ previous next ]

Q1349 How can I check that a string contains a date?

Try the following:

Note that it doesn't check to see if the date is a valid date, i.e. the 30th Feb 2000 does not exist, but it still parses as a valid date string. If you need to check that the date exists see the answer to FAQ #1 - How do you check if a date is valid?.

<script language="JavaScript"><!--
function validateDate(date) {
    // NaN is never equal to itself.
    if (Date.parse(date) != Date.parse(date))
        document.write(date + ' - Invalid date<br>');
    else
        document.write(date + ' - Valid date<br>');
}


validateDate('hello world');
validateDate('29 January 1999');
validateDate('February 30 2000');
validateDate('Sat 8 2000 Jan');
//--></script>

Feedback on 'Q1349 How can I check that a string contains a date?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.