-1

I am try get month and day but value is different o my text:

var fech = new Date("2021-02-28T00:00:00");
document.writeln("Year=" + fech.getFullYear());
document.writeln("Month=" + fech.getMonth());
document.writeln("Day=" + fech.getDay());

Result is:

Year=2021 Month=1 Day=0

https://jsfiddle.net/j0qn2jjr/3/

Daxtron2
  • 1,239
  • 1
  • 11
  • 19
Cris Loli
  • 71
  • 1
  • 3
  • 2
    1) Your fiddle doesn't match your code in the post. The fiddle is getting the day correctly based on what it is being passed 2) Months are zero-indexed. (UPDATE:) 3) As Máté states below, `getDate` gets the date in the month, not `getDay`. – Alexander Nied Apr 03 '18 at 15:14

2 Answers2

2

getDay() returns the day of the week (Sunday is 0, Monday is 1, etc). What you're probably looking for is getDate().

Also, getMonth() returns 0-11 (January is 0, February is 1, etc).

Read up here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Máté Safranka
  • 4,081
  • 1
  • 10
  • 22
0

GetMonth() returns the number month, which in this instance in Feb, using a 0 index, Feb would actually be month 1 in this scenario. Use getMonth()+1 if you need to get the numbers.

@Máté Safranka has explained about the day problem too!

James Lingham
  • 419
  • 1
  • 3
  • 17