- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2019 01:40 AM
Hello,
Can any one please suggest a solution to get a latest date from an array of dates using server side code? The array of dates is as per the below format:
var array = ["2019-08-28","2019-08-29","2019-09-06"];
Expected output: 2019-09-06
- Prabhh
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2019 01:49 AM
Hi
Try this below code in background script -
function max_date(all_dates) {
var max_dt = all_dates[0],
max_dtObj = new Date(all_dates[0]);
all_dates.forEach(function(dt, index)
{
if ( new Date( dt ) > max_dtObj)
{
max_dt = dt;
max_dtObj = new Date(dt);
}
});
return max_dt;
}
gs.print(max_date(["2019/08/28","2019/08/29","2019/09/06"]));
Let me know if it helps.
Regards,
Omkar Mone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2019 01:46 AM
You can use plain javascript for it (https://stackoverflow.com/a/7143443)
array.reduce(function (a, b) { return a > b ? a : b; });

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2019 01:49 AM
Hi
Try this below code in background script -
function max_date(all_dates) {
var max_dt = all_dates[0],
max_dtObj = new Date(all_dates[0]);
all_dates.forEach(function(dt, index)
{
if ( new Date( dt ) > max_dtObj)
{
max_dt = dt;
max_dtObj = new Date(dt);
}
});
return max_dt;
}
gs.print(max_date(["2019/08/28","2019/08/29","2019/09/06"]));
Let me know if it helps.
Regards,
Omkar Mone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2019 03:07 AM
Hello Omkar,
I tried the mentioned script and it worked perfectly, however, I had to change the format of the dates to yyyy/MM/dd.
Thank you for the help.
-Prabhh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2019 03:21 AM
Hi
I'm sorry for that, here is the code that would work with your format -
function max_date(all_dates) {
var max_dt = all_dates[0],
max_dtObj = new GlideDateTime(all_dates[0]);
all_dates.forEach(function(dt, index)
{
if ( new GlideDateTime( dt ) > max_dtObj)
{
max_dt = dt;
max_dtObj = new GlideDateTime(dt);
}
});
return max_dt;
}
gs.print(max_date(["2019-08-28","2019-08-29","2019-09-06"]));