getMonth in javascript gives previous month

JavascriptDate

Javascript Problem Overview


I am using a datepicker which gives a date in the format Sun Jul 7 00:00:00 EDT 2013. Even though the month says July, if I do a getMonth, it gives me the previous month.

var d1 = new Date("Sun Jul 7 00:00:00 EDT 2013");
d1.getMonth());//gives 6 instead of 7

What am I doing wrong?

Javascript Solutions


Solution 1 - Javascript

Because getmonth() start from 0. You may want to have d1.getMonth() + 1 to achieve what you want.

Solution 2 - Javascript

getMonth() function is zero indexed based. You need to do d1.getMonth() + 1

Recently I used Moment.js library and never looked back. Try it!

Solution 3 - Javascript

Presuming you use your variable
var d1 = new Date("Sun Jul 7 00:00:00 EDT 2013");
Month requires a +1 to be accurate, it starts counting at 0
d1.getMonth() + 1 // month 

###In contrast.... these methods DON'T need a plus 1

d1.getSeconds()   // seconds 
d1.getMinutes()   // minutes 
d1.getDate()      // date    

###And notice it is .getDate() NOT .getDay()

d1.getDay()       // day of the week as a 
Hope this helps

I suspect these methods lack consistency for historical reasons

Solution 4 - Javascript

const d = new Date();
const time = d.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', second:'numeric', hour12: true });
const date = d.toLocaleString('en-US', { day: 'numeric', month: 'numeric', year:'numeric' });

OR

const full_date = new Date().toLocaleDateString(); //Date String
const full_time = new Date().toLocaleTimeString(); // Time String

Output

Date = 8/13/2020

Time = 12:06:13 AM

Solution 5 - Javascript

Yes, this seems to have been a boneheaded decision on someone's part to make the month zero-indexed while the year and day are not. Here's a little function I use to convert a date into the format expected for the field...

const now = new Date()
const month = (date) => {
    const m = date.getMonth() + 1;
    if (m.toString().length === 1) {
        return `0${m}`;
    } else {
        return m;
    }
};
const day = (date) => {
    const d = date.getDate();
    if (d.toString().length === 1) {
        return `0${d}`;
    } else {
        return d;
    }
};

const formattedDate = `${now.getFullYear()}-${month(now)}-${day(now)}`

Solution 6 - Javascript

you can also find current month in this way

const today = new Date()
const getMonth = (today.getMonth() + 1).toString().length === 1 ? `0${today.getMonth() + 1}`:today.getMonth() + 1 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondeveloper747View Question on Stackoverflow
Solution 1 - JavascriptRahul TripathiView Answer on Stackoverflow
Solution 2 - JavascriptletiagoalvesView Answer on Stackoverflow
Solution 3 - JavascriptjasonleonhardView Answer on Stackoverflow
Solution 4 - JavascriptMuradView Answer on Stackoverflow
Solution 5 - JavascriptBaron YoungView Answer on Stackoverflow
Solution 6 - Javascriptparag barsarView Answer on Stackoverflow