使用新的Date()获取月份范围

时间:2016-12-01 09:51:05

标签: javascript jquery

    foreach($tickets as $t){
        $use = $t->user;
        $us = User::where('_id', '=', $use)->get();
        foreach($us as $a)
        {
            $username=$a->name;
            $t['name']=$username;
        }
    }
    foreach($tickets as $t) {
        $use1 = $t->user_assigned;
        $us1 = User::where('_id', '=', $use1)->get();
        foreach ($us1 as $a1) {
            $username1 = $a1->name;
            $t['namea'] = $username1;
        }
    }  

尝试获取月份的第一天和每月的最后一天来制定月度报告。我得到function firstDayOfMonth(given_month) { var d = new Date(); d.setMonth(given_month, 1); return d.toISOString(); } function lastDayOfMonth(given_month) { var d = new Date(); d.setMonth(given_month + 1, 0); return d.toISOString(); } var temp = { firstDayOfMonth: firstDayOfMonth(given_month), lastDayOfMonth: lastDayOfMonth(given_month) } console.log(JSON.stringify(temp))  对于临时,这是正常的吗?为什么它是9:45的东西?

2 个答案:

答案 0 :(得分:0)

你正在做正确的事。您只需要为开始和结束日设置小时:

firstDayOfMonth.setHours(0,0,0,0);
lastDayOfMonth.setHours(23,59,59,999);

答案 1 :(得分:0)

将H / M / S设置为12:00:00的日期对象标准化,如下所示:

const givenMonth = 6;

const normalizeTime = d => {
    d.setHours(12);
    d.setMinutes(0);
    d.setSeconds(0);
    return d;
};

const getISODate = (month, day) => {
    const d = new Date();
    d.setMonth(month, day);
    return normalizeTime(d).toISOString();
};

const temp = {
    firstDayOfMonth: getISODate(givenMonth, 1),
    lastDayOfMonth: getISODate(givenMonth + 1, 0)
};

temp;