- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:13 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 09:31 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 09:23 PM - edited 12-12-2023 09:40 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 09:31 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 02:20 PM
Bala - this is a very elegant solution and it works great! Thanks!