GetMonth in javascript gives previous month

Asked
Active3 hr before
Viewed126 times

8 Answers

givesjavascript
90%

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.,Because getmonth() start from 0. You may want to have d1.getMonth() + 1 to achieve what you want., 2 Don't lose your mind with dates in javascript. Use Moment.js if you can – Valamas Sep 4 '13 at 21:48 ,getMonth() function is zero indexed based. You need to do d1.getMonth() + 1

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 
load more v
88%

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var d = new Date();
var monthName = months[d.getMonth()]; // "July" (or current month)
72%

The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year). , An integer number, between 0 and 11, representing the month in the given date according to local time. 0 corresponds to January, 1 to February, and so on. , The second statement below assigns the value 11 to the variable month, based on the value of the Date object Xmas95. , Note: If needed, the full name of a month (January for example) can be obtained by using Intl.DateTimeFormat() with an options parameter. Using this method, internationalization is made easier:

getMonth()
load more v
65%

Returns the Month in the specified date according to local time.,Javascript date getMonth() method returns the month in the specified date according to local time. The value returned by getMonth() is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.,Javascript - Multimedia,Javascript - Validations

Its syntax is as follows −

Date.getMonth()
load more v
75%

To get the previous month name, first we need to access the current date object using the new Date() constructor.,In this tutorial, we are going to learn about how to get the previous month name using the current date in JavaScript.,Now, we need to subtract the current month with -1 and set it to the current date using the setMonth()and getMonth() methods.,setMonth() : The setMonth() sets the month to a date object.

const current = new Date();
load more v
40%

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.,Because getmonth() start from 0. You may want to have d1.getMonth() + 1 to achieve what you want.,Seems to be a bug. If the string sent to Date() is formatted as 2012/03/21 instead of 2012-03-21. The date seems right.,The reason for this is that today is the 29th. Since your date object is set to the current day implicitly and February only had 28 days in 2013, you see March printed twice. Fix this by setting the optional day parameter:

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
load more v
22%

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.,Because getmonth() start from 0. You may want to have d1.getMonth() + 1 to achieve what you want.,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...,getMonth() function is zero indexed based. You need to do d1.getMonth() + 1

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
load more v
60%

For some reason the getMonth method returns the number of the month starting with 1 but the Date function expects the number of the month starting with 0. So when you input the getMonth result you automatically get last month.,Setting the day to 0 returns the last day of the month.,While in the javascript tab of Forms you can press F12 and choose the console tab to see if there is any errors after you click the Save button. This helps when your javascript appears to not be doing anything at all.,Does anyone have a simple JavaScript solution for a calendar field in my table? I just want that field to default to the last day of the previous month (whatever that may be). Thanks!

Try,

$(document).ready(function() {
   var dateObj = new Date();
   dateObj.setDate(0)

   var month = dateObj.getMonth() + 1;
   var day = dateObj.getDate();
   var year = dateObj.getFullYear();

   newdate = month + "/" + day + "/" + year;

   $('#q3 input').val(newdate);

})
load more v

Other "gives-javascript" queries related to "GetMonth in javascript gives previous month"