js基础——Date

 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 +'">&nbsp;</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+'">&nbsp;</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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值