Get week, month and year from date

Pratiksha Lang1
Kilo Sage

Get week, month and year from date

I have written a code below to get the week, month and year from date but is not working for month of February

 

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


var prefixes = ['1st', '2nd', '3rd', '4th', '5th'];

 

var gdt = new GlideDateTime('2023-02-28');


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


var day=gdt.getDayOfMonthLocalTime();


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

3 REPLIES 3

Naga Ravindra R
Kilo Sage

Hi @Pratiksha Lang1 

 

May be use the GlideDate() and GlideDateTime() api menthods, which are available in the below link:

Check here 

For Ex: I have a date,
For getting Year:
var gdt = new GlideDateTime(); //2011

gs.print(gdt.getYear());

 

For week:

var gdt = new GlideDateTime("2011-12-01 12:00:00");

gs.print(gdt.getWeekOfYearUTC());

 

Please respond, if there are any questions.

Please mark as helpful/correct if it helps. Thank you

@Naga Ravindra R  This gives me week number of entire year from 1 to 53, I want it to take it as monthly weeks from 1 to 5 

for example : if date is 2023-02-20

The output should be :

week - 4th week (as it is 4th week of February and not year)

month - February

year - 2023

Hi @Pratiksha Lang1 ,

 

May I ask if you were able to find the solution for your requirement?

 

I have similar use case.

 

Thanks in advance.

 

Regards, Vincent Wong