How to convert date format in Flow Designer?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I need to convert the date format to yyyy-mm-dd in flow designer using a inline script
my trigger is a problem task record which contains a due_date field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Kiran_25
https://www.servicenow.com/community/developer-forum/format-date-in-flow-designer/m-p/2720379
Try the script below.
var dt = fd_data._1__get_catalog_variables.recording_ends_date;
var gd = new GlideDate();
gd.setValue(dt);
return "Recording on:" + gd.getByFormat('MM-dd-YYYY');
Regards
Dr Atul G. - Learn N Grow Together ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
******************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @Kiran_25,
Building off of the previous comment, the code for your specific question would look something like the following:
var dueDate = fd_data.trigger.current.due_date; // grabs the due_date field from off your problem task record trigger
var gd = new GlideDate();
gd.setValue(dueDate);
var formattedDate = gd.getByFormat('yyyy-MM-dd');
You can find the full list of format options here:
https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html
If you wanted to expand that formatting to include any timestamp information, you could do that via the following:
var dueDate = fd_data.trigger.current.due_date; // grabs the due_date field from off your problem task record trigger
var gdt = new GlideDateTime();
gdt.setValue(dueDate);
var gd = gdt.getDate(); // or getLocalDate() depending on your use case
var gt = gdt.getTime(); // or getLocalTime() depending on your use case
var formattedDate = gd.getByFormat('yyyy-MM-dd') + " " + gt.getByFormat('HH:mm:ss');
Helpful resources:
GlideDateTime Docs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
var dueDateStr = fd_data.trigger.current.due_date;
if (dueDateStr) {
var gdt = new GlideDateTime(dueDateStr);
return gdt.getDate().getByFormat('yyyy-MM-dd');
}
return ''; // Return empty string if due_date is empty
Hope this helps!
Thank You!
Juhi Poddar
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @Kiran_25 ,
There are 2 approaches either try this script :
var dueDate = fd_data.trigger.problem_task.due_date;
if (!dueDate) return '';
var gdt = new GlideDateTime(dueDate);
return gdt.getDate().getByFormat('yyyy-MM-dd');
OR
Utilized a transform function on the data pill :
If my response helped mark as helpful and accept the solution.
