How to get the week number and month name and year using javascript or with any other way

suprakash
Giga Expert

I need to get the week number that 1st week or 2nd week, and the month name and the year . The month name and year i can do it using javascript but i am not able to understannd how to get the week number . Suppose if I run the code now as it is 2nd week of May it must display like This is 2nd Week of Month May : 2017

I got the month name and year by this code

var monthNames = ',January,February,March,April,May,June,July,August,September,October,November,December'.split(',');

var gdt = new GlideDateTime();

var month=(monthNames[new GlideDate().getMonthLocalTime()]+":"+gdt.getYear());

I am not able to get the week number.Need help..

1 ACCEPTED SOLUTION

Bryan Tay3
Mega Guru

hi Suprakash,



here you go.



var monthNames = ',January,February,March,April,May,June,July,August,September,October,November,December'.split(',');


var prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];



var gdt = new GlideDateTime();


var month=(monthNames[gdt.getMonthLocalTime()]+":"+gdt.getYear());


var day=gdt.getDayOfMonthLocalTime();


gs.log('This is '+prefixes[0 | day / 7]+' Week of Month '+month);



Hope this helps.


View solution in original post

4 REPLIES 4

Karthik Reddy T
Kilo Sage

Hellow Suprakash,



Refer the below link may helpful to you.



http://wiki.servicenow.com/index.php?title=GlideDateTime#gsc.tab=0



http://wiki.servicenow.com/index.php?title=GlideSystem_Date_and_Time_Functions#gsc.tab=0


Karthik Reddy T.
ServiceNow Commnunity MVP -2018 class.

Bryan Tay3
Mega Guru

hi Suprakash,



here you go.



var monthNames = ',January,February,March,April,May,June,July,August,September,October,November,December'.split(',');


var prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];



var gdt = new GlideDateTime();


var month=(monthNames[gdt.getMonthLocalTime()]+":"+gdt.getYear());


var day=gdt.getDayOfMonthLocalTime();


gs.log('This is '+prefixes[0 | day / 7]+' Week of Month '+month);



Hope this helps.


Where do I need to apply this? I would also like to display the week numbers in my reports.

@Bryan Tay3  This code doesn't work for month of February, Can you please help. if I take a date as 2023-02-27, it shows week 4th, but it is 5th week