- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2020 05:17 PM
I have an array of objects:
var array = [ {date: "2018-08-26T00:00:00Z"}, {date: "2020-08-26T00:00:00Z"}, {date: "2017-08-26T00:00:00Z"} ]
...and trying to reorder the objects in the array by "date" DESC.
I found this post which has a great solution in it, and the solution works with the dates provided in the array from that post: Community Post however if you swap out the array of dates for the ones I need to work with I can't get it to work. Here is a snippet of code from what I am trying to do:
var array = [ {date: "2018-08-26T00:00:00Z"}, {date: "2020-08-26T00:00:00Z"}, {date: "2017-08-26T00:00:00Z"} ]
array.sort(function(a, b) {
var dateA=new Date(a.date), dateB=new Date(b.date)
return dateA-dateB;
});
gs.print(array[0].date)
If I run this code against a Node server, it works great.
I have troubleshot this a lot, so providing the simplest version of what I am trying to solve less all the noise and millions of iterations of attempts. 🙂 Anyone have any idea on how to make this logic work with the dates that I am working with?
Thanks in advance!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2020 12:58 AM
I dont think "new Date()" takes "2017-08-26T00:00:00Z" as a valid format.
But it should be possible to sort the array by:
var array = [ {date: "2018-08-26T00:00:00Z"}, {date: "2020-08-26T00:00:00Z"}, {date: "2017-08-26T00:00:00Z"} ]
array.sort(function(a, b) {
var date1 = new GlideDateTime();
date1.setValueUTC(a.date.replace("T", " ").replace("Z", ""), "yyyy-MM-dd HH:mm:ss");
var date2 = new GlideDateTime();
date2.setValueUTC(b.date.replace("T", " ").replace("Z", ""), "yyyy-MM-dd HH:mm:ss");
if (date1.getValue() < date2.getValue())
return 1;
if (date1.getValue() > date2.getValue())
return -1;
return 0;
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2020 06:23 PM
Hi,
Sorry, I just wanted to double-check, so does the post you linked work or not work for you?
Because you said it works for you, then you said it doesn't work.
So I wasn't sure if you were just looking for alternative method, but you already have a solution, or perhaps misstated that it works for you because it doesn't?
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2020 06:55 PM
Hi
Hopefully that makes sense, will also update my original post so it is less confusing.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2020 12:38 AM
Hi Shaun,
considering you only want to compare dates and not time; below script should help you
try to run in background and check once
var str = '[{"date": "2018-08-26T00:00:00Z"}, {"date": "2020-08-26T00:00:00Z"}, {"date": "2017-08-26T00:00:00Z"}]';
var parser = JSON.parse(str);
var array = [];
var length = parser.length;
for(var i=0;i<length;i++){
var value = parser[i].date;
var gdt = new GlideDateTime(value);
array.push(gdt.getDate().toString());
}
var latest = array[0];
for(var i=0;i<array.length;i++){
if (GlideDateTime(array[i]) > GlideDateTime(latest))
latest = array[i];
}
gs.info('Latest Date from array is:' + latest);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2020 12:58 AM
I dont think "new Date()" takes "2017-08-26T00:00:00Z" as a valid format.
But it should be possible to sort the array by:
var array = [ {date: "2018-08-26T00:00:00Z"}, {date: "2020-08-26T00:00:00Z"}, {date: "2017-08-26T00:00:00Z"} ]
array.sort(function(a, b) {
var date1 = new GlideDateTime();
date1.setValueUTC(a.date.replace("T", " ").replace("Z", ""), "yyyy-MM-dd HH:mm:ss");
var date2 = new GlideDateTime();
date2.setValueUTC(b.date.replace("T", " ").replace("Z", ""), "yyyy-MM-dd HH:mm:ss");
if (date1.getValue() < date2.getValue())
return 1;
if (date1.getValue() > date2.getValue())
return -1;
return 0;
});