date.setHours() not working

后端 未结 5 367
一生所求
一生所求 2021-01-19 08:17

I am trying to subtract hours from a given date time string using javascript. My code is like:

     var cbTime = new Date();    
     cbTime = selectedTime.s         


        
5条回答
  •  滥情空心
    2021-01-19 09:12

    The reason is that setHours(), setMinutes(), etc, take an Integer as a parameter. From the docs:

    ...

    The setMinutes() method sets the minutes for a specified date according to local time.

    ...

    Parameters:

    An integer between 0 and 59, representing the minutes.

    So, you could do this:

    var selectedTime = new Date(),
        cbTime = new Date(); 
       
    cbTime.setHours(selectedTime.getHours() - 5);
    cbTime.setMinutes(selectedTime.getMinutes() - 30);
    
    document.write('cbTime: ' + cbTime);
    document.write('
    '); document.write('selectedTime: ' + selectedTime);

提交回复
热议问题