DateTool & SortTool
In Short: I would like to sort nodes by a specific date field, and ignore the dates which are older than "today".
I look for the specific event nodes...
#set ( $eventlist = $_XPathTool.selectNodes($contentRoot, "//system-data-structure/Event") )
I then try to sort the nodes by the date field (a custom
field)...
$_SortTool.addSortCriterion("/system-data-structure/Event/date", "", "number", "ascending", "")
$_SortTool.sort($eventlist)
This does not seem to work at all
I run through a loop and set each variable...
#set ( $today = $_DateTool.getSystemDate() )
#set ( $datecheck = $_DateTool.toDate("dd-MM-yyyy",$eventDate) )
#set ( $difference = $_DateTool.difference($today, $datecheck).getSeconds() )
Each of these print as...
today = Fri Mar 13 11:30:33 CDT 2015
datecheck = Thu Nov 03 00:00:00 CDT 2016
difference = 51884966
So, as expected, this does not work at all...
#if ( $_DateTool.difference($today, $datecheck).getSeconds() > 1 )
Am I on the right track?
Discussions are closed to public comments.
If you need help with Cascade CMS please
start a new discussion.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by Chris on 13 Mar, 2015 05:39 PM
Full Velocity Script:
2 Posted by Ryan Griffith on 13 Mar, 2015 05:43 PM
Hi Chris,
Thank you for attaching the Format and sample XML. To confirm, is it the sorting by the date field that is not working for you?
Thanks!
3 Posted by Chris on 13 Mar, 2015 05:47 PM
Neither the sorting nor the datetool are working for me. Of course I have it commended out above (that is not the issue) :)
I have a feeling that my times are not in the correct format, but they are matching!
4 Posted by Chris on 13 Mar, 2015 05:52 PM
Sorry, should have done this in the first place...these are the guys that are not cooperating...
and
5 Posted by Ryan Griffith on 13 Mar, 2015 06:11 PM
Thank you for clarifying, Chris.
One issue I am seeing is the date format you are providing in the following line has the day and month switched:
It should be:
Give this a try and let me know if that addresses the Date Tool issue.
Regarding the sort issue, I noticed you are specifying the data type as
number
in the criterion, but you are passing a string. Try changing that totext
to see if it resolves the issue.Please let me know if you have any questions.
Thanks!
6 Posted by Chris on 13 Mar, 2015 08:17 PM
Well, having the correct formatting would make sense, thanks! :)
I now have it working, here is what I did...
Sorting Tool
Because I define where the node is
//system-data-structure/Event
I can just calldate
instead of//system-data-structure/Event/date
in the sortingtool.DateTool
Referring back to a different post I found earlier today, I decided to try the "convert to number" method...it works!
Saying that, I still have an odd issue. The sort tool only seems to work with dates that are part of the same year. So the sorting tool must be sorting by month, day, and then year. For instance:
Now the question is, how might I correct this?
Thanks for all your help today Ryan!
* 02-25-2015
7 Posted by Ryan Griffith on 16 Mar, 2015 03:32 PM
Hi Chris,
You are definitely going to have issues sorting on the string. Normally, you can split it out into different sort criterion; however, there is a known defect that will prevent you from doing so.
That being said, what I generally recommend is to create an array of Map objects with keys that can be sorted on by the Sort Tool. Give the following a try and let me know how it works out:
Please let me know if you have any questions.
Thanks!
8 Posted by Chris on 26 Mar, 2015 06:23 PM
Figured it out: use UNIX time!
Calendar
date selector to aDate/Time
selector in my Data Definition.MM-DD-YYYY
date to UNIX time, a large string of seconds$_SortTool.addSortCriterion("date", "", "number", "ascending", "upper-first")
#set ( $today = $_DateTool.getSystemDate() )
#set ( $todaynum = $_NumberTool.toNumber($today) )
9 Posted by Ryan Griffith on 26 Mar, 2015 08:16 PM
Thank you for following up, Chris. I am glad to hear you were able to come up with a much better solution that won't involve multiple loops.
I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.
Have a great day!
Ryan Griffith closed this discussion on 08 Apr, 2015 02:43 PM.