Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap

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

You are here: irt.org | FAQ | JavaScript | Date | Q1349 [ previous next ]

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>');
        document.write(date + ' - Valid date<br>');

validateDate('hello world');
validateDate('29 January 1999');
validateDate('February 30 2000');
validateDate('Sat 8 2000 Jan');

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

©2018 Martin Webb