Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto populate the month and week base on a selected date

darianistor
Tera Contributor

Hi. 

Please help me populate the month and week fields based on the date I select for the day field. 

darianistor_0-1707400949054.png

 

1 ACCEPTED SOLUTION

Amit Pandey
Kilo Sage

Please use the following code snippet and make necessary changes-

var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
var gdt = new GlideDateTime();
var month = monthNames[gdt.getMonthLocalTime()] + ' ' + gdt.getYear();
var weekNumber = Math.floor((gdt.getDayOfMonthLocalTime() - 1) / 7);
var weekPrefix = weekNumber < prefixes.length ? prefixes[weekNumber] : '';
gs.log('This is ' + weekPrefix + ' Week of Month ' + month);

View solution in original post

2 REPLIES 2

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @darianistor 

https://www.servicenow.com/community/developer-forum/extract-month-year-from-date-into-a-string/m-p/...

 

https://www.servicenow.com/community/itsm-forum/how-to-get-the-week-number-and-month-name-and-year-u...

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Amit Pandey
Kilo Sage

Please use the following code snippet and make necessary changes-

var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
var gdt = new GlideDateTime();
var month = monthNames[gdt.getMonthLocalTime()] + ' ' + gdt.getYear();
var weekNumber = Math.floor((gdt.getDayOfMonthLocalTime() - 1) / 7);
var weekPrefix = weekNumber < prefixes.length ? prefixes[weekNumber] : '';
gs.log('This is ' + weekPrefix + ' Week of Month ' + month);