Home > OS >  TypeError: midnight.getTime is not a function
TypeError: midnight.getTime is not a function

Time:06-20

I want to get the remaining time until 0 o'clock but I get an error midnight.getTime () is not a function

My Code:

let midnight = new Date().setHours(0, 0, 0, 0)
let cooldown = new Date(midnight.getTime() - Date.now())
console.log(`${cooldown.getHours()}:${cooldown.getMinutes()}:${cooldown.getSeconds()}`)

CodePudding user response:

The setHours function of Date returns timestamp(in ms) which is a number and doesn't have a getTime method. So you should first store the new Date() in a variable and then set the time using setHours on it. Code:

let midnight = new Date();
midnight.setHours(0, 0, 0, 0);

CodePudding user response:

new Date().setHours(0, 0, 0, 0) gives a number. You should convert it to a Date.

See the reference.

  • Related