JavaScript DHTML/Date Time/Date Parse

Материал из Web эксперт
Перейти к: навигация, поиск

Date parse Example

   <source lang="html4strict">

   

<html> <body> <button onclick="var myDate = new Date();

                alert(Date.parse(myDate));">Date: PARSE</button>

</body> </html>


 </source>
   
  


Find a date string with regular expression

   <source lang="html4strict">
 

<html> <head> <title>Find Date</title> </head> <body> <script type="text/javascript"> var regExp = /:\D*\s\d+\s\d+/; var str = "This is a date: March 12 2008"; var resultString = str.match(regExp); document.writeln("Date" + resultString); </script> </body> </html>


 </source>
   
  


Use regular expression to check a date

   <source lang="html4strict">
 

<HTML> <HEAD> <TITLE>A date</TITLE> <SCRIPT> function checkDate(testStr) {

  var pattern = /\b(\d{2})\/(\d{2})\/(\d{4})\b/;
  var result = testStr.match(pattern);
  if (result != null)
     return "It is a date!";
  else
     return "Not a date.";

} </SCRIPT> </HEAD> <BODY> <FORM name="theForm">

Enter a date in mm/dd/yyyy format: <INPUT type=text name=testStr size=20 maxlength=10> <INPUT type=button name="theButton" value="Verify the Format" onClick="alert(checkDate(document.theForm.testStr.value))";> </FORM> </BODY> </HTML> </source>