Example 1
Direct use date for comparison.
The following code compares the current date with the August 9, 2008:
| The code is as follows |
Copy Code |
<script type= "Text/javascript" > var mydate=new Date (); Mydate.setfullyear (2008,7,9); var today = new Date (); if (mydate>today) { Alert ("Today is not until August 9, 2008!") ”); } Else { Alert ("August 9, 2008 has passed, Wake up!") ”); } </script> |
Example 2
Convert a time date to a number using split-cut to compare
JS Time comparison (Yyyy-mm-dd hh:mi:ss)
| The code is as follows |
Copy Code |
| function Comptime () { var begintime = "2009-09-21 00:00:00"; var endtime = "2009-09-21 00:00:01"; var begintimes = begintime.substring (0). Split ('-'); var endtimes = endtime.substring (0). Split ('-'); BeginTime = begintimes[1] + '-' + begintimes[2] + '-' + begintimes[0] + ' + begintime.substring (10, 19); Endtime = endtimes[1] + '-' + endtimes[2] + '-' + endtimes[0] + ' + endtime.substring (10, 19); Alert (begintime + "AAA" + endtime); Alert (Date.parse (Endtime)); Alert (Date.parse (begintime)); var a = (Date.parse (endtime)-Date.parse (BeginTime))/3600/1000; if (a < 0) { Alert ("Endtime small!"); else if (a > 0) { Alert ("Endtime big!"); else if (a = = 0) { Alert ("Equal time!"); } else { Return ' exception ' } } |