Trying to get the current time in JavaScript

35 Views Asked by At
const hours = now.getHours();
Console.log(hours);

These set of code is giving me...

Uncaught ReferenceError: now is not Defined

Note: This is a JavaScript code

I try to use w3school as reference and Google to check the built-in function for time in JavaScript, still I it the same sa now.getHours(); but didn't run

1

There are 1 best solutions below

0
shaedopotato On

To add an answer here - what you have written implies that "now" is already declared and that it has a method .getHours() which would give you the hours. Date objects have that method, so it might be a good idea to define "now" as a constant, then use the Date object method getHours() to log the hours at the time you declared the date.

e.g.,

const now = new Date();
const hours = now.getHours();
console.log(hours);