JS实现日期比较
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>测试</title>
8 <script type="text/javascript" language="javascript">
9
10 Date.prototype.format = function (formatStr) {
11 var date = this;
12 var zeroize = function (value, length) {
13 if (!length) {
14 length = 2;
15 }
16 value = new String(value);
17 for (var i = 0, zeros = ''; i < (length - value.length); i++) {
18 zeros += '0';
19 }
20 return zeros + value;
21 };
22 return formatStr.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHmstT])\1?|[lLZ])\b/g, function($0) {
23 switch ($0) {
24 case 'd': return date.getDate();
25 case 'dd': return zeroize(date.getDate());
26 case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][date.getDay()];
27 case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
28 case 'M': return date.getMonth() + 1;
29 case 'MM': return zeroize(date.getMonth() + 1);
30 case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getMonth()];
31 case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][date.getMonth()];
32 case 'yy': return new String(date.getFullYear()).substr(2);
33 case 'yyyy': return date.getFullYear();
34 }
35 });
36 }
37
38 function CompareDate()
39 {
40 var curDate=document.getElementById("txtCurDate").value;
41 var dgDate=document.getElementById("txtDgDate").value;
42
43 var value=daysBetween(dgDate,curDate);
44
45
46 if (value<=0)
47 alert("到港");
48 else
49 alert("在途");
50
51 return value;
52
53 }
54
55 function daysBetween(DateOne,DateTwo)
56 {
57 var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
58 var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
59 var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
60
61 var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
62 var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
63 var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
64
65 var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
66 return cha;
67 }
68
69 </script>
70 </head>
71 <body>
72 <form id="form1" runat="server">
73 <div>
74 当前日期:<input id="txtCurDate" type="text" /> 到港日期:<input id="txtDgDate"
75 type="text" value="2012-05-10" /><input type="button" value="日期比较" onclick="CompareDate()" /></div>
76 </form>
77
78 <script language="javascript" type="text/javascript">
79 document.getElementById("txtCurDate").value= new Date().format("yyyy-MM-dd");
80 </script>
81 </body>
82 </html>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>测试</title>
8 <script type="text/javascript" language="javascript">
9
10 Date.prototype.format = function (formatStr) {
11 var date = this;
12 var zeroize = function (value, length) {
13 if (!length) {
14 length = 2;
15 }
16 value = new String(value);
17 for (var i = 0, zeros = ''; i < (length - value.length); i++) {
18 zeros += '0';
19 }
20 return zeros + value;
21 };
22 return formatStr.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHmstT])\1?|[lLZ])\b/g, function($0) {
23 switch ($0) {
24 case 'd': return date.getDate();
25 case 'dd': return zeroize(date.getDate());
26 case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][date.getDay()];
27 case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
28 case 'M': return date.getMonth() + 1;
29 case 'MM': return zeroize(date.getMonth() + 1);
30 case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getMonth()];
31 case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][date.getMonth()];
32 case 'yy': return new String(date.getFullYear()).substr(2);
33 case 'yyyy': return date.getFullYear();
34 }
35 });
36 }
37
38 function CompareDate()
39 {
40 var curDate=document.getElementById("txtCurDate").value;
41 var dgDate=document.getElementById("txtDgDate").value;
42
43 var value=daysBetween(dgDate,curDate);
44
45
46 if (value<=0)
47 alert("到港");
48 else
49 alert("在途");
50
51 return value;
52
53 }
54
55 function daysBetween(DateOne,DateTwo)
56 {
57 var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
58 var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
59 var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
60
61 var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
62 var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
63 var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
64
65 var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
66 return cha;
67 }
68
69 </script>
70 </head>
71 <body>
72 <form id="form1" runat="server">
73 <div>
74 当前日期:<input id="txtCurDate" type="text" /> 到港日期:<input id="txtDgDate"
75 type="text" value="2012-05-10" /><input type="button" value="日期比较" onclick="CompareDate()" /></div>
76 </form>
77
78 <script language="javascript" type="text/javascript">
79 document.getElementById("txtCurDate").value= new Date().format("yyyy-MM-dd");
80 </script>
81 </body>
82 </html>