Turn time into number

Asked

Viewed 59 times

0

I have this problem: I can get the time correctly and show everything normal, but when I try to turn the time into number, it returns me NaN:

var hora = new Date()

var horaAtual = hora.getHours()

var nHoraAtual = Number(horaAtual.value)

var minuto = new Date()

var minutoAtual = minuto.getMinutes()

var nMinutoAtual = Number(minutoAtual.value)

var segundo = new Date()

var nSegundoAtual = Number(nMinutoAtual.value)
  • Why horaAtual.value and not just horaAtual?

  • From what I know . value is a way to transform the number with value

  • Bro, when you come in with .getHours() comes in number. Print type with typeof horaAtual and you will see on the console the result "number".

  • 1

    The return of Date.prototype.getHours will already be an integer between 0 and 23 inclusive. It is not necessary to convert it to Number.

  • 1

    This applies to other functions: getMinutes(), getSeconds(), etc..

  • Just to show how to pick up each item: regraData: any = new Date();
 regraDia: any = new Date().getDate();
 regraMes: any = new Date().getMonth() + 1;
 regraHora: any = new Date().getHours();
 regraMinuto: any = new Date().getMinutes(); The month has to add "+1" because... I don’t know why, but to come the right date I do so.

  • Ah yes, I understood Anderson Carlos Woss! Convert the timeAtual to Number was giving this problem, now I withdrew and it worked! Thanks

  • Something else, you don’t have to create new Date() all the time. You can create regraData = new Date() at the beginning and then only use regraData.getDate(), regraData.getHours(), etc.

Show 3 more comments

2 answers

1

These commands will take the amount you want:

  var regraData = new Date();
  var regraDia = new Date().getDate();
  var regraMes = new Date().getMonth() + 1;
  var regraHora = new Date().getHours();
  var regraMinuto = new Date().getMinutes();
  var regraSegundo = new Date().getSeconds();

The month has "+1" because the month count starts at "0".

EDIT: As explained by @hkotsubo, it is best to put the date in a variable and call this variable to set the other information:

  var regraData = new Date();
  var regraDia = regraData.getDate();
  var regraMes = regraData.getMonth() + 1;
  var regraHora = regraData.getHours();
  var regraMinuto = regraData.getMinutes();
  var regraSegundo = regraData.getSeconds();

1

All methods getXXX of a Date return numbers (see documentation of getHours(), for example), so you don’t need to use Number, value nor any other trick to convert to number, because by calling these methods, you will already have a number.

Another detail is that you don’t have to spend all the time creating new Date(), just create once at the beginning and use it to get the other values:

let data = new Date();
let dia = data.getDate();
let mes = data.getMonth() + 1;
let ano = data.getFullYear();
let hora = data.getHours();
let minuto = data.getMinutes();
let segundo = data.getSeconds();
let millis = data.getMilliseconds();

console.log(`ano=${ano}, mês=${mes}, dia=${dia}, hora=${hora}, minuto=${minuto}, segundo=${segundo}, milissegundos=${millis}`);

Of course if you use new Date() several times also works in most cases. But remember that each time you create a new instance, it will have the current date and time of the instant it was created.

Depending on how the code runs, it can give different results: the first and last execution may be a few milliseconds apart, or even seconds apart. And if the code runs too close to midnight, even the day will be different.

That is, let’s assume that the code started running on 09/25/2019 at 23:59:59.999 - then this line gets the value 25:

let dia = new Date().getDate();

Then, 1 thousandth of a second later, the code arrived on the line that picks up the schedule:

let hora = new Date().getHours();

As it has already passed 1 millisecond, new Date() returns a date for 09/26/2019 at midnight, and so hora has the value zero. Then your program would show that it is midnight (zero hour) of the day 25 (which is the value of the day variable).

Yeah, it’s a corner case, "may be "never" that happens, but if you create a new instance of Date Every time, there’s a chance it could happen. If you want to show all data referring to the same date, it is best to create only one at the beginning and use all getters without creating a new one unnecessarily. Or use some other formatting method.

As for the value of the month, I had to add 1 because in Javascript months are indexed to zero (January is zero, February is 1, etc).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.