getDay () returns me 1 in JavaScript

1

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.

    
asked by FrEqDe 29.05.2018 в 03:46
source

2 answers

1

That happens because you have two ways to get the day:

  • getDate (); You get it in a format from 1 to 31
  • getDay (); You get it in a format from 0 to 6
  • 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

        
    answered by 29.05.2018 / 03:57
    source
    1

    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
    
        
    answered by 29.05.2018 в 03:50