Get week from date through script in servicenow

Neeraja2
Tera Contributor

Hello Everyone,

I need to get week from a date in a month. Not in a year.

For example, 

for example : if date is 2024-02-05

The output should be :

week - 1st week (as it is 1st week of February and not year)

 

Please anyone suggest script on this? Thanks in advance

10 REPLIES 10

Shaqeel
Mega Sage

Hi @Neeraja2 

 


In ServiceNow, you can use the GlideDateTime API to get the number of weeks in a month from a date. Here's a sample script:

javascript
var gdt = new GlideDateTime(); // get current date and time
gdt.setValue('2022-02-01'); // set a specific date
var month = gdt.getMonthValue(); // get the month value
var year = gdt.getYear(); // get the year value

// calculate the number of days in the month
var daysInMonth = new Date(year, month, 0).getDate();

// calculate the number of weeks in the month
var weeksInMonth = Math.ceil(daysInMonth / 7);

gs.info('Number of weeks in the month: ' + weeksInMonth);


This script will output the number of weeks in the month of the specified date.

Here are the steps:

1. Create a new GlideDateTime object.
2. Set a specific date using the setValue method.
3. Get the month and year values using the getMonthValue and getYear methods.
4. Calculate the number of days in the month using the Date object.
5. Calculate the number of weeks in the month by dividing the number of days by 7 and rounding up using the Math.ceil function.
6. Output the number of weeks in the month using the gs.info function.

 

kindly mark helpful/solution.

 

Regards

Shaqeel


***********************************************************************************************************************
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

Shaqeel

Neeraja2
Tera Contributor

Thanks for this, Shaqeel.

But when I try to run this script in background script, not is not working and giving nan as week.

Neeraja2_0-1707144600543.png

 

Hi @Neeraja2 

 

Okay, Let me try on my end and will get back to you.

 

Thanks & Regards

Shaqeel


***********************************************************************************************************************
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

Shaqeel

OlaN
Giga Sage
Giga Sage

Hi,

There is no easy way to get that data.

I can suggest that you extract the week by looping the date, removing a week at a time and check if you are still in the same month, and by that calculate which week of the month it is.

But there are some pit-falls still, as in your example, if the date is 2024-02-05, is it then the first or the second week, since the first week starts on a different day? Teoretically the  1st to the 4th Feb is week 1, and the 5th Feb is week 2 if you allow for "halv-weeks".

Providing a simple pseudo-code example below, on how I would do.

create a GDT date from your date input
set a variable on which month the date is on
create a counter to count the weeks

do a loop
  as long as the GDT date is still on the same month
  remove one week from the GDT object
  add to the counter
  check if were still on the same month
  break out of the loop if on the previous month

Use the counter variable to say which week of the month it is