The CreatorCon Call for Content is officially open! Get started here.

Unable to extract week from a date when using variable

gjz
Mega Sage

I have a business requirement to extract the week of the year, month and day from a given date in a catalog item.  This will be done in a script in a workflow.

 

However, I'm not getting a value back, just "undefined" and I don't understand why.  

 

Here is my test script:

var current = new GlideRecord('sc_req_item');
current.addQuery('sys_id', '3b1742ea1b43b9104cf78622604bcb51');
current.query();
if (current.next()) {
    var dateOnly = new GlideDate();
    var dateAll = new GlideDateTime();

    dateOnly = current.variables.date_only.getDisplayValue();
    dateAll = dateOnly + ' 12:00:00';
    gs.print('dates: ' + dateOnly + ' || ' + dateAll);
    gs.print('week: ' + dateAll.getWeekOfYearLocalTime());

    var sDate = new GlideDate();
    var tDate = new GlideDateTime('2024-01-01 12:00:00');
    gs.print('string Date: ' + tDate);
    gs.print('str week: ' + tDate.getWeekOfYearLocalTime());

}
 
And this is what I get when I run the script:
gjz_0-1702572563276.png

 

Does anyone know why it works when I pass it a string but not the variable?  I've tried converting the variable to a string, but it doesn't make a difference, it still has the value "undefined".

2 REPLIES 2

Saurabh Gupta
Kilo Patron

Hi, 

You can find as below

var gdt=new GlideDateTime(dateOnly);
gs.info(gdt.getWeekOfYearLocalTime())

Thanks and Regards,

Saurabh Gupta

Amit Verma
Kilo Patron
Kilo Patron

Hi @gjz 

 

Considering you will get the date from a catalog variable, you can tweak your existing code as below to make it work :

var dateOnly = new GlideDateTime('2024-01-08');
 gs.print('dates: ' + dateOnly);
 gs.print('week: ' + dateOnly.getWeekOfYearLocalTime());

This will give you the week number as an output. Here is the output snip for your reference :

 

AmitVerma_0-1702622630122.png

Please mark the answer as helpful and correct if it answers your query.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.