js不同的变量相同的值

时间:2018-09-18 22:12:59

标签: javascript

当前,我面临以下问题。在我的JavaScript代码中,我有一个函数,该函数应该计算从现在开始到给定时间戳记的星期数。问题在于,第一个值与另一个具有另一个名称的变量具有相同的值。代码:

let myAppointment = event.data.toTime;
            console.log(myAppointment);
            let currentDate = new Date(Date.now());
            let currentTime = (convertStringDateToDate(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 0,0,0,0)).getTime();
            let appointmentStartDate = new Date(myAppointment.getStart());
            console.log(appointmentStartDate);
            console.log(currentDate);
            let appointmentStartTime = (convertStringDateToDate(appointmentStartDate.getFullYear(), appointmentStartDate.getMonth(), appointmentStartDate.getDate(),0,0,0,0)).getTime();
            console.log('AppointmentStartTime : ' + appointmentStartTime);
            console.log('CurrentTime: ' + currentTime);
            let timeskip = appointmentStartTime - currentTime;
            console.log(timeskip + ' timeskip / 604800000 = ' + (timeskip / 604800000));
            skips = timeskip / 604800000;
            await displayCalendar(document.getElementById('wrapper'));
            console.log(skips);
            if(skips < 0){
                skips = Math.floor(skips);
                if(Math.abs(skips) != 1){
                    navigateBackward(skips);
                }

            }else{
                skips = Math.floor(skips);
                if(Math.abs(skips) != 1){
                    navigateForward(skips);
                }
            }
            cleanTable();
            displayAppointments();
});

//i think this function may be interesting too, but the error can't occur from here
function
convertStringDateToDate(year,month,day,hours,minutes,seconds,milliseconds){
    let date = new Date();
    date.setFullYear(year);
    date.setMonth(month);
    date.setDate(day);
    date.setHours(hours);
    date.setMinutes(minutes);
    date.setSeconds(seconds);
    date.setMilliseconds(milliseconds);
    return date;
}

let myAppointment = ...中的函数导致以下控制台输出 enter image description here

2 个答案:

答案 0 :(得分:0)

请注意,在您的特定示例中,Traceback (most recent call last): File "/Users/bliu4/.local/bin/sam", line 11, in <module> sys.exit(cli()) File "/Users/bliu4/anaconda3/lib/python3.6/site-packages/click/core.py", line 722, in __call__ return self.main(*args, **kwargs) File "/Users/bliu4/anaconda3/lib/python3.6/site-packages/click/core.py", line 676, in main _verify_python3_env() File "/Users/bliu4/anaconda3/lib/python3.6/site-packages/click/_unicodefun.py", line 118, in _verify_python3_env 'for mitigation steps.' + extra) RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult http://click.pocoo.org/python3/for mitigation steps. currentDate共享相同的年份,月份和日期。调用appointmentStartDate时,应将let currentTime = (convertStringDateToDate(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 0,0,0,0)).getTime(); 0,0,0,0的正确值替换为currentTime

要使编码风格保持一致,可以使用appointStartTimedate.getHours()date.getMinutes()date.getSeconds()替换这四个零。

此外,如果您想缩短代码,则可以摆脱date.getMilliseconds(),而只需在convertStringDateToDate()getTime()上调用currentDate

appointStartDate

答案 1 :(得分:0)

问题出在这里:

 let appointmentStartTime = (convertStringDateToDate(appointmentStartDate.getFullYear(), appointmentStartDate.getMonth(), appointmentStartDate.getDate(),0,0,0,0)).getTime();

您将小时,分钟,秒都设置为零-有效地将开始日期更改为与今天的日期相同的值。