Get week, month and year from date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2023 11:52 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2023 12:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2023 12:12 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 04:20 AM
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