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).
Why
horaAtual.value
and not justhoraAtual
?– Woss
From what I know . value is a way to transform the number with value
– Victor Lorena
Bro, when you come in with
.getHours()
comes in number. Print type withtypeof horaAtual
and you will see on the console the result"number"
.– Leonardo Getulio
The return of
Date.prototype.getHours
will already be an integer between 0 and 23 inclusive. It is not necessary to convert it toNumber
.– Woss
This applies to other functions:
getMinutes()
,getSeconds()
, etc..– Leonardo Getulio
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.– Edward Ramos
Ah yes, I understood Anderson Carlos Woss! Convert the timeAtual to Number was giving this problem, now I withdrew and it worked! Thanks
– Victor Lorena
Something else, you don’t have to create
new Date()
all the time. You can createregraData = new Date()
at the beginning and then only useregraData.getDate()
,regraData.getHours()
, etc.– hkotsubo