new GlideDateTime().getMonth not working properly

gayatri6
Tera Contributor

I need to get the current months of the 2 dates.  

I am trying to use the getMonthLocalTime(), getMonthUTC() and getMonth() so as to get the current month of the date entered. If the date is larger than 12, then it is working fine and returning the month number. HOWEVER, if the date is less than 13, i.e., 1-12, then it is getting confused and returning the date of month number instead of the month number. What should I do?

 

 

10 REPLIES 10

So its clear that there is issue with your system date or instance date format.

Day should be = 5

and Month should be = 7

Did you tried converting it as Ankur mentioned?

Can you show us your current SI?

Thank you
Prasad

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Remember getMonth() is not there in scoped app. check what Prasad has mentioned are you in scope application when you are running the script

I tried with date which is less than 13 i.e. between 1-12 and it worked fine in global scope

I hope you are setting the date in the correct format in GlideDateTime

I took an example of today which is 5th July and it gave me 7 as the output

var gdt = new GlideDateTime();

gs.info('Month->' + gdt.getMonth());

[0:00:00.057] Script completed in scope global: script


Script execution history and recovery available here


*** Script: Month->7

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I am doing in script include and sending the date from client script.

Can you show us your client script and script include?

Hi,

so there is issue in the format you are using in the script include

example below for format conversion which might help you

Below will convert date which is in yyyy/dd/MM format to MM/dd/yyyy format

var MyConversion = Class.create();
MyConversion.prototype = {
	initialize: function() {
	},

	convertDateFormat: function(){

		var incomingDate = this.getParameter('sysparm_format') + ' 00:00:00';
		var format = 'yyyy/dd/MM HH:mm:ss';
		var gdt = new GlideDateTime();
		gdt.setDisplayValue(incomingDate,format);
		var gd = new GlideDate(); 
		gd.setValue(gdt.getDate());
		return gd.getByFormat("MM/dd/yyyy");

	},

	type: 'MyConversion'
};

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader