cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

this function returns previous month and not the current month

Level 7

this function returns previous month and not the current month

var currentdate = new Date();

currentdate.getMonth();

 

For example, this return 5 instead of 6.

5 REPLIES

Re: this function returns previous month and not the current month

Hi Asekar,

 

getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.

https://www.w3schools.com/jsref/jsref_getmonth.asp

Re: this function returns previous month and not the current month

Thanks for the response. I got the point.

Now what is the best way to get the current month ?

Re: this function returns previous month and not the current month

The month number would be : currentdate.getMonth()+1

Are you looking for the month Name (January, ...) ? Do you want it localized ?

Re: this function returns previous month and not the current month

I don’t want the month name.

I tried +1 and this works fine

But when I use a parsedate function, the date where I’m storing the currendate treats month section as a string and not numeric

For example, 51 instead of 6

Re: this function returns previous month and not the current month

Is there a need to extract the month from the date? Or is it just a problem where the month value is being read wrong when you use parsedate function?

 

If it is the second one, then you could try changing the format string in the parsedate function. I believe Thingworx uses joda-time for the date functions internally. So you can try a format like this: 

var result = parseDate(date, "dd/MM/yyyy");

This would make the parse date function search for days of the month (01-31), Month of the year (01-12), Year. Confusion between using mm and MM is very common.

 

If this doesn't work you could maybe share a snippet of your code, so everyone can understand better what is it your trying to do here.