1 <html>
2 <body>
3 <script language=javascript>
4 Date.prototype.format = function(format)
5 {
6 var o =
7 {
8 "M+" : this.getMonth()+1, //month
9 "d+" : this.getDate(), //day
10 "h+" : this.getHours(), //hour
11 "m+" : this.getMinutes(), //minute
12 "s+" : this.getSeconds(), //second
13 "q+" : Math.floor((this.getMonth()+3)/3), //quarter
14 "S" : this.getMilliseconds() //millisecond
15 }
16 if(/(y+)/.test(format))
17 format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
18 for(var k in o)
19 if(new RegExp("("+ k +")").test(format))
20 format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
21 return format;
22 }
23
24 function showWeekFirstDay()
25 {
26 var Nowdate=new Date();
27 //alert(Nowdate);
28 //alert(Nowdate.getDay());
29 var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1+42)*86400000);
30 alert(WeekFirstDay.format('yyyyMMdd'))
31 year=WeekFirstDay.getYear()
32 month=WeekFirstDay.getMonth()+1
33 date=WeekFirstDay.getDate()
34 alert(year+""+month+""+date)
35 alert(WeekFirstDay.getMonth())
36 alert(WeekFirstDay.toDateString())
37 }
38
39 function showWeekLastDay()
40 {
41 var Nowdate=new Date();
42 var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
43 var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
44 alert(WeekLastDay)
45 }
46
47 function showMonthFirstDay()
48 {
49 var Nowdate=new Date();
50 var MonthFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth(),1);
51 alert(MonthFirstDay)
52 }
53
54 function showMonthLastDay()
55 {
56 var Nowdate=new Date();
57 var MonthNextFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth()+1,1);
58 var MonthLastDay=new Date(MonthNextFirstDay-86400000);
59 alert(MonthLastDay.format('yy-MM-dd hh:mm:ss'))
60 }
61
62
63 Date.prototype.pattern=function(fmt) {
64 var o = {
65 "M+" : this.getMonth()+1, //月份
66 "d+" : this.getDate(), //日
67 "h+" : this.getHours() == 0 ? 12 : this.getHours(), //小时
68 "H+" : this.getHours(), //小时
69 "m+" : this.getMinutes(), //分
70 "s+" : this.getSeconds(), //秒
71 "q+" : Math.floor((this.getMonth()+3)/3), //季度
72 "S" : this.getMilliseconds() //毫秒
73 };
74
75 var week = {
76 "0" : "\u65e5",
77 "1" : "\u4e00",
78 "2" : "\u4e8c",
79 "3" : "\u4e09",
80 "4" : "\u56db",
81 "5" : "\u4e94",
82 "6" : "\u516d"
83 };
84
85 if(/(y+)/.test(fmt)){
86 fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
87 }
88 if(/(E+)/.test(fmt)){
89 fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getDay()+""]);
90 }
91 for(var k in o){
92 if(new RegExp("("+ k +")").test(fmt)){
93 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
94 }
95 }
96 return fmt;
97 }
98
99 </script>
100 <input name=haha type=button value="本周第一天" onclick=showWeekFirstDay()>
101 <input name=haha type=button value="本周第七天" onclick=showWeekLastDay()>
102 <input name=haha type=button value="本月第一天" onclick=showMonthFirstDay()>
103 <input name=haha type=button value="本月最后一天" onclick=showMonthLastDay()>
104 </body>
105 </html>