Skip to content Skip to sidebar Skip to footer

Javascript Date Function Showing Wrong Date

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.getDay(); var month=date.getMonth(); var year=date.getY

Solution 1:

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

Solution 2:

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()

Post a Comment for "Javascript Date Function Showing Wrong Date"