new GlideDateTime().getMonth not working properly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2021 11:55 PM
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?
- Labels:
-
Change Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2021 04:57 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2021 12:08 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2021 12:50 AM
I am doing in script include and sending the date from client script.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2021 12:53 AM
Can you show us your client script and script include?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2021 02:40 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader