如何使用moment.js获取基于月末和月初的每周时间?

时间:2020-06-04 14:55:29

标签: javascript reactjs logic momentjs

我试图找出如何使用moment.js在不同的星期之间切换并做出反应。基本上,当我按向左箭头键时,时间回溯一周,而向右箭头则指向未来。到目前为止,我已经完成了工作。还有一个重要的细节。该时间段应始终在月底结束,即使只是一周的中旬,然后下一个周期从周中开始,直到星期日结束。因此,有可能在一周内有两个月期,但永远不会超过两个月。到目前为止,我已经尝试过:

    var [displayWeek, setDisplayWeek] = useState(currWeekStart.format('MMM DD, YYYY') + ' - ' + currWeekEnd.format('MMM DD, YYYY'));
    var [periodSwitch, setPeriodSwitch] = useState(false);
    var [weekInFocus, setWeekInFocus] = useState(currWeekStart);

    useEffect(() => {
        if (displayWeek) {
            document.getElementById('periodDisplay').value = displayWeek;
        }
    }, [displayWeek])

    function setPeriod(prevOrNext) {
        if(periodSwitch === true){
            console.log("switching period in month");
            setWeekInFocus(weekInFocus.clone().endOf('week').startOf('month'));
            setPeriodSwitch(false);
        }
        else{
            if(prevOrNext === 'prev'){
                console.log("wif on prev before " + weekInFocus.format('MMM DD, YYYY'));
                setWeekInFocus(weekInFocus.clone().subtract(1, 'week'));
                console.log("wif on prev after " + weekInFocus.format('MMM DD, YYYY'));
            }
            else{
                setWeekInFocus(weekInFocus.add(1, 'week'));
            }
        }

        console.log(weekInFocus.clone().startOf('week'));
        console.log(weekInFocus.clone().endOf('week'));
        var weekOverlapsMonths = false;

        if(weekInFocus.clone().startOf('week').get('month') !== weekInFocus.clone().endOf('week').get('month') && periodSwitch === false){
            console.log("start "+weekInFocus.clone().startOf('week').get('month'));
            console.log("end " + weekInFocus.clone().endOf('week').get('month'));
            weekOverlapsMonths = true;
            setPeriodSwitch(true);
            console.log("set pswitch " + periodSwitch);
        }

        console.log("fw " + weekInFocus.format('MMM DD, YYYY'));

        var displayWeekStart = "";
        var displayWeekEnd = "";

        if(periodSwitch === true){
            if(prevOrNext === 'prev'){
                displayWeekEnd = weekInFocus.clone().endOf('month').format('MMM DD, YYYY');
                console.log("pswitch prev");
            }
            else{
                displayWeekStart = weekInFocus.clone().startOf('month').format('MMM DD, YYYY');
                console.log("pswitch next");
            }
        }
        else{
            displayWeekStart = weekInFocus.clone().startOf('week').format('MMM DD, YYYY');
            console.log("pswitch else");
        }

        if(weekOverlapsMonths === true){
            if(prevOrNext === 'prev'){
                displayWeekEnd = weekInFocus.clone().startOf('week').endOf('month').format('MMM DD, YYYY');
                console.log("weekoverl prev");
            }
            else{
                displayWeekEnd = weekInFocus.clone().startOf('week').endOf('month').format('MMM DD, YYYY');
                console.log("weekoverl next");
            }
        }
        else{
            displayWeekEnd = weekInFocus.endOf('week').format('MMM DD, YYYY');
            console.log("weekoverl else");
        }

        setDisplayWeek(displayWeekStart + ' - ' + displayWeekEnd)
    }

0 个答案:

没有答案