I have the following code to get the date.
var f = new Date();
f.getDay();
and when I want to get the day with the getDay()
method I get the value 1 but today it is 28.
I have the following code to get the date.
var f = new Date();
f.getDay();
and when I want to get the day with the getDay()
method I get the value 1 but today it is 28.
That happens because you have two ways to get the day:
So if you want to show the date you should do the following
const dia = new Date().getDate()
console.log(dia)
Now if what you are looking for is to obtain that location in the week has a specific day, then do it like this
const dia = new Date().getDay()
console.log(dia)
You must understand that in the case of getDay () the days are counted as if it were an array in position 0: Sunday, position 1: Monday and so on successively until you reach 6
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|--------|-------|-------|-------|-------|-------|-------|
|Domingo | Lunes | Martes| Mierc | Jueves| Viern | Sabado|
On the other hand, getDate () will give you the number within the calendar corresponding to the date, that is, today is 28 and therefore that is the day it returns to you
What happens is that Date.getDay()
returns the day of the week, not month 1 would correspond to Monday, if the default calendar starts on Sunday, to get the day of the month you have to use Date.getDate()
.
let fecha = new Date();
console.log(fecha.getDate()); //devolvera la fecha actual