This is a discussion on Check that a string contains a date. within the HTML, CSS and Javascript Coding Techniques forums, part of the Web Development category; Hi all... Can anyone tell me how can i check that a string contains a date. Thanks in advance......
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi all... <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> Thanks... |
| |||
| <script language="JavaScript"><!-- function y2k(number) { return (number < 1000) ? number + 1900 : number; } function isDate (day,month,year) { // checks if date passed is valid // will accept dates in following format: // isDate(dd,mm,ccyy), or // isDate(dd,mm) - which defaults to the current year, or // isDate(dd) - which defaults to the current month and year. // Note, if passed the month must be between 1 and 12, and the // year in ccyy format. var today = new Date(); year = ((!year) ? y2k(today.getYear()):year); month = ((!month) ? today.getMonth():month-1); if (!day) return false var test = new Date(year,month,day); if ( (y2k(test.getYear()) == year) && (month == test.getMonth()) && (day == test.getDate()) ) return true; else return false } if (isDate(31,2,1997)) document.write("Valid"); else document.write("Invalid"); //--></script> |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Check if a date is valid. | itbarota | HTML, CSS and Javascript Coding Techniques | 3 | 04-10-2008 04:04 PM |
| Convert String to Date: new Date(string) | oxygen | Flash Actionscript Programming | 3 | 03-07-2008 09:24 PM |
| Convert a date in milliseconds back to a date string | itbarota | HTML, CSS and Javascript Coding Techniques | 2 | 03-03-2008 09:03 PM |
| How do you check if a date is valid? | itbarota | HTML, CSS and Javascript Coding Techniques | 4 | 12-19-2007 12:06 AM |
| How can I compare a date in a string with todays date? | itbarota | HTML, CSS and Javascript Coding Techniques | 2 | 12-18-2007 11:02 PM |