nodejs 周操作

var Rangedate = require('rangedate');

Date.prototype.normalize = function () {
    return this.toLocaleDateString() + " 00:00:00";
};


Date.prototype.otherDay = function (day) {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate() + day, this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
};
Date.prototype.durationDay = function (day) {
    var s = new Date(this.normalize());
    return Rangedate(s, day).map(function (data) {
        return new Date(data.normalize());
    });
};

function getMondayByToday(today) {
    var _weekDay = today.getDay();//0,1,2,3,4,5,6

    _weekDay = (_weekDay == 0) ? (7) : _weekDay;
    var mondayDateThisWeek = today.otherDay(-(_weekDay - 1));

    return mondayDateThisWeek;
}

function getWeekDurationDayPoolByToday(today) {
    var monday = getMondayByToday(today);
    var sunday = monday.otherDay(6);

    var weekPool = monday.durationDay(sunday);
    return weekPool;
}

function theWeekOfYear(curDate) {
    /*
     date1是当前日期
     date2是当年第一天
     d是当前日期是今年第多少天
     用d + 当前年的第一天的周差距的和在除以7就是本年第几周
     */
    var a = curDate.getFullYear();
    var b = curDate.getMonth() + 1;
    var c = curDate.getDate();

    var date1 = new Date(a, parseInt(b) - 1, c), date2 = new Date(a, 0, 1),
        d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
    return Math.ceil(
        (d + ((date2.getDay() + 1) - 1)) / 7);
}



var weekPool = getWeekDurationDayPoolByToday(new Date());

console.log("weekPool:",weekPool);

console.log(theWeekOfYear(new Date("2016-2-1")))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值