Skip to content

Commit cbc838d

Browse files
committed
DatePicker update
日历数据获取
1 parent db58438 commit cbc838d

File tree

3 files changed

+176
-41
lines changed

3 files changed

+176
-41
lines changed

.idea/workspace.xml

Lines changed: 107 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DatePicker/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<meta charset="UTF-8">
55
<title>DataPicker组件开发</title>
66
<link rel="stylesheet" href="css/base.css">
7+
78
</head>
89
<body>
910
<div class="ui-datepicker-wrapper">
@@ -75,5 +76,10 @@
7576
</table>
7677
</div>
7778
</div>
79+
<script src="js/base.js"></script>
80+
<script>
81+
var monthDate = datepicker.getMonthData(2016,12);
82+
console.log('monthDate',monthDate);
83+
</script>
7884
</body>
7985
</html>

DatePicker/js/base.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
11
/**
22
* Created by Jonathan Zhang on 2017/5/19.
33
*/
4+
(function () {
5+
var datepicker = {};
6+
7+
datepicker.getMonthData =function (year,month) {
8+
var ret = [];
9+
10+
if(!year || !month){
11+
var today = new Date();
12+
year = today.getFullYear();
13+
month = today.getMonth()+1;
14+
}
15+
16+
/*这个月的第一天*/
17+
var firstDay = new Date(year,month-1,1);
18+
/*这个第一天是周几*/
19+
var firstDayWeekDay = firstDay.getDay();
20+
/*周日赋值为7*/
21+
if(firstDayWeekDay === 0) firstDayWeekDay = 7;
22+
23+
/*上个月的最后一天*/
24+
var lastDayOfLastMonth = new Date(year,month -1,0);
25+
/*存储上个月的最后一天*/
26+
var lastDateOfLastMonth = lastDayOfLastMonth.getDate();
27+
28+
/*第一行要显示多少个上个月的日期*/
29+
var preMonthDayCount = firstDayWeekDay - 1;
30+
31+
/*当月的最后一天*/
32+
var lastDay = new Date(year,month,0);
33+
var lastDate = lastDay.getDate();
34+
35+
/*获取六周日期*/
36+
for (var i=0;i<7*6;i++){
37+
var date = i + 1 - preMonthDayCount;
38+
var showDate = date;
39+
var thisMonth = month;
40+
41+
if(date<=0){
42+
//上一月
43+
thisMonth = month - 1;
44+
showDate = lastDateOfLastMonth + date;
45+
}else if(date > lastDate){
46+
//下一月
47+
thisMonth = month + 1;
48+
showDate = showDate -lastDate;
49+
}
50+
51+
if(thisMonth === 0) thisMonth = 12;
52+
if(thisMonth === 13) thisMonth = 1;
53+
54+
ret.push({
55+
month:thisMonth,
56+
date:date,
57+
showDate:showDate
58+
})
59+
}
60+
61+
return ret;
62+
63+
};
64+
window.datepicker = datepicker;
65+
}
66+
)();

0 commit comments

Comments
 (0)