new Date(2014,8,1).getDay()
1.刚好得出9月离星期天的天数;
new Date(2014,8, 0).getDate();
eg:20140900相当于201408的天数;
2.刚好得出本月的天数;有个疑问是:setDate()不是从1-31?怎有0的设置?这是偶合?
为了写个calendar做准备:
今天写了修正当前单数的月份为多位,例如'8'->'08';8->008
function digitDate1(number,count) {
var _curnum = number+"";
var _factNum = count- _curnum.length;
for(var i = 0;i < _factNum;i++){
_curnum = "0" + _curnum;
return _curnum;
}
}
function digitDate2(number,count) {
var _curnum = number+"";
var _factNum = count- _curnum.length;
for(var i = 0;i < _factNum;i++)
_curnum = "0" + _curnum;
return _curnum;
}
console.log(digitDate2(9,3))
犯了个很可爱的错误,每次写for循环,总是来个括号。导致误认为这两个函数是一样的;
今天深深的认识到写代码要有规范:变量名与=要有空格;等等。。。。
函数二
//20140901--2014年9月1日;
function stringToDate(string){
return new Date(
parseInt(string.substring(0,4)),
parseInt(string.substring(4,5)) == '0' ? parseInt(string.substring(5,6))-1 : parseInt(string.substring(4,6))-1,
parseInt(string.substring(6,7)) == '0' ? parseInt(string.substring(7))-1 : parseInt(string.substring(6))-1
)
}
注意的是这个函数是将20140901--2014年9月1日;
函数三
//2014年09月01日--20140901
function dayToString(date) {
return date.getFullYear() + digitDate(date.getMonth()+1,2) + digitDate(date.getDate(),2) ;
}
函数四
//是否是今天
function isToday(date) {
return isSameDay(date,new Date());
}
function isSameDay(a,b) {
return a.getFullYear() == b.getFullYear()
&& a.getMonth() == b.getMonth()
&& a.getDate() == b.getDate();
}
调用的时候注意传入与Date的对象类型
函数五
//设置唯一标识
function setMoudleId() {
var MoudleId=''
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for(var i = 0 ; i < 32; i++){
MoudleId += chars.charAt(Math.random()*62);
}
return MoudleId
}
函数六
//基本的calendar的函数
//是否是闰年
isLeapYear:function(year) {
return (year%4 == 0 && year%100 !== 100) || (year%400 == 0)
},
isStartTime:function() {
return this.isSameDay(
new Date(this.currentTime.getFullYear(),this.currentTime.getMonth(),1),
new Date(this.properties.startTime.getFullYear(),this.properties.startTime.getMonth(),1)
)
},
isEndTime:function() {
return this.isSameDay(
new Date(this.currentTime.getFullYear(),this.currentTime.getMonth(),1),
new Date(this.properties.endTime.getFullYear(),this.properties.endTime.getMonth(),1)
)
},
//设置当前的时间,闰年为29天
setCurrentDate:function(year,month) {
this.dateArray[1] = isLeapYear(year) ? 29 :28;
this.currentTime.year = year;
this.currentTime.month = month;
},
showPreTime:function() {
if (this.isStartTime()) return;
if (this.currentTime.month < 1) {
this.currentTime.month = 11;
this.currentTime.year--;
}
else this.currentTime.month--;
this.reader(this.currentTime.year, this.currentTime.month);
},
showNextTime:function() {
if (this.isEndTime()) return;
if (this.currentTime.month > 11) {
this.currentTime = 0;
this.currentTime.year++;
}
else {
this.currentTime.month++;
}
this.reader(this.currentTime.year, this.currentTime.month);
},
preRender:function(year,month) {
setCurrentDate(year,month);
this.dataCollection.length = 0;
},
renderHear:function() {
var html = '';
var prevMonthClass = this.isStartTime() ? this.properties.prevMonthClass : this.properties.prevMonthActiveClass;
var nextMonthClass = this.isEndTime() ? this.properties.nextMonthClass : this.properties.nextMonthActiveClass;
html += '<table class="'
+this.properties.headerClass+'"><tr><td id="'
+this.properties.prevMonthClass+this._MODULE_ID_ +'" class="'
+prevMonthClass +'"> </td><td class="'
+this.properties.currentTimeClass+'">'
+this.properties.monthEnlistName[this.currentTime.month] +' '
+this.currentDate.year
+'</td><td id="'
+this.properties.nextMonthClass+this._MODULE_ID_+'" class="'
+nextMonthClass+'"> </td></tr></table>';
return html;
},
renderWeekdays:function() {
var html = '';
for (var i in this.properties.weekdayName) {
html += '<td> '+this.properties.weekdayName[i] +'</td>'
}
return '<tr class="'+this.properties.weekdayClass+'">'+html+'</tr>'
},
主要是把功能分开写,把每个模块用render();