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

How can I get the 2 digit day from a GlideDate in a workflow script?

gjz
Mega Sage

I've been struggling with this for quite awhile and I haven't seen a solution on the community that works for me.

 

I have a catalog item with a date variable.  In the workflow, I need to parse the month and day from the date, but the month and day needs to be two digits.  How can I script that?

 

Example:

2024-01-02 (YYYY-MM-DD)  is the variable's value.  I need the value 0102 in my script, not 12.

1 ACCEPTED SOLUTION

BalaG
Kilo Sage

Hello @gjz  you can use the slice string function as follows

var gdt = new GlideDateTime('2024-01-02 12:00:00' );
gs.print(('00'+ gdt.getMonth()).slice(-2) + ('00'+ gdt.getDayOfMonth()).slice(-2));

output -- 0102

 

Hope that helps

--

Bala Guthy

Please mark this as helpful or a solution as appropriate

View solution in original post

7 REPLIES 7

Amit Verma
Kilo Patron
Kilo Patron

Hi @gjz 

 

 

You can use below JavaScript logic to get the required values :

 

 

var date = '2024-01-02';
date = date.split('-');
var month = date[1];
var day = date[2] ;
gs.print(month);
gs.print(day);

 

Please mark the answer as correct and helpful if it solves your problem.

 

Thanks & Regards

Amit


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

BalaG
Kilo Sage

Hello @gjz  you can use the slice string function as follows

var gdt = new GlideDateTime('2024-01-02 12:00:00' );
gs.print(('00'+ gdt.getMonth()).slice(-2) + ('00'+ gdt.getDayOfMonth()).slice(-2));

output -- 0102

 

Hope that helps

--

Bala Guthy

Please mark this as helpful or a solution as appropriate

Bala - this is a very elegant solution and it works great!  Thanks!