How can I calculate the last day of the month? getDaysInMonthUTC() doesn't work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2022 06:46 AM
Hii!
I need to do the calculation that returns true 3 days before the end of the month. This was my idea:
var today = new GlideDateTime();
today = today.getDate();
var daysInMonth = new GlideDateTime();
daysInMonth.getDaysInMonthUTC();
var daysDiff = GlideDateTime.subtract(today, daysInMonth);
if (daysDiff == 3){
answer = true;
}
but it is not working, I tried with today's date and that the dateDiff == 14 inside the if and it returns me 1970-01-01 13:44:50, any ideas?
- Labels:
-
Team Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2022 06:52 AM
HI
I have not the proper code for you, but I always use the following approach: get a GlideDateTime object with the first day of the next month and then subtract 1 day.
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2022 07:00 AM
Hi Maik! That could work for me, what method do you use to calculate the first day of the next month?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2022 06:58 AM
Hi,
Please try below, I have tested in PDI and its working fine.
vvar today = new GlideDateTime();
var todayDateInteger = today.getDayOfMonthUTC();
var lastDateInteger=today.getDaysInMonthUTC();
var diff=lastDateInteger-todayDateInteger
if (diff== 3){
answer = true;
}
Let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Thanks and Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2022 07:00 AM
Hi,
like this
var gdt = new GlideDateTime();
var num = gdt.getDaysInMonthUTC();
var gdt = new GlideDateTime();
gdt.setDayOfMonthLocalTime(num);
gs.info(gdt);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader