The code below shows the day, month and year as:
6
3
116
Which is obviously wrong dates.
var date= new Date();
var day=date.get
getDay()
returns the day of the week from 0-6 for Sun-Sat,
getMonth()
returns the month from 0-11, so 3 means April,
getYear()
has been deprecated and replaced with getFullYear()
which should be used instead.
It just looks like all of these functions do something different than what you were expecting.
To get day of the month from 1-31: getDate()
,
To get month like you were expecting, just add 1: getMonth() + 1
You're querying the wrong functions and misunderstanding the output.
getDay()
returns the day of the week.
getMonth()
returns the month, but January starts with 0.
getYear()
returns the year minus 1900
You are probably looking for:
getDate()
, getMonth()+1
, getFullYear()
Encoding in javascript for months from is 0-11 (not 1-12).
For year, you could use getFullYear() instead of getYear()
"Date.prototype.getFullYear() Returns the year (4 digits for 4-digit years) of the specified date according to local time."
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date