Simulating DateDiff with metadata and/or system dates?


(Tim Davison) #1

Hi all

 

Does anyone know of a way to simulate a datediff function using metadata dates and/or comparing to current time?

 

e.g.

We want to display a "New!" icon at the top of the page if it has been published in the last week, but after that time the icon not display.

 

I couldn't find a way to do this using keyword replacement/modifiers.  Was wondering if anyone else knows a way.  %asset_published_relative% looks promising, and the online manuals show this type of syntax in an example:

%globals_date_relative_this_friday_d-m-Y%

see http://manuals.matrix.squizsuite.net/concepts/appendices/global-keyword-replacements#Current-Date-and-Time

 

though I could not find any more info about how to use this and it prints some pretty neat gobbeldeegook that was otherwise pretty useless (at least as far as I could tell).  The _relative keywords seem to be designed to be human readable, rather than for programmatic comparison.

 

I supposed what I'm trying to find is if there is a datediff equivalent.

 

I have 2 other options for solving this:

1> Use Javascript (probably the easiest)

2> Create an additional metadata field, gets set by a trigger every time it is published to a week in advance, and use that as the comparison (I don't like this approach - seems too heavy-handed).

 

Was just wondering if there was a way using just keywords and modifiers.

 

Edit: we are on Matrix v4.12.4


(Anewport) #2

Not exactly working to relative dates like you had planned and I have no idea if this will work since our system is below 4.16.1 and the gte_date keyword modifier is not supported below that version. You also need to have a system that can support keyword replacements values. All this stuff is on http://manuals.matrix.squizsuite.net/keyword-replacements/chapters/keyword-modifiers

 

What if you had a metadata field that used the default value as a keyword of Last Published Date and offset by +1 week in the Offset field. Then on the front end print out that metadata field and check to see if its value is greater than the actual published date, and if yes, show the "new" icon. So it would be something like:

%asset_metadata_published^gte_date:{asset_published_short}:NEW:OLD%

Not sure how the metadata date field needs to be formatted for the gte_date keyword to understand a date but if it must be formatted as YYYY-MM-DD you could just set its format on the schema or modify it too:

%asset_metadata_published^date_format:Y-m-d^gte_date:{asset_published_short}:NEW:OLD% 

Let me know if that actually works  :D


(Tim Davison) #3

Thanks for response chickensoup.  I've updated my post to indicate it's Matrix v4.12.4, so as you found cannot test the solution you've given, but looks like it should work.

 

Something that I was playing around with was the following:

%globals_date_U^subtract:604800^replace_keywords:lt:{asset_published_U}:OLD:NEW%

The U format is seconds since epoch, so should be dealing with straight numbers.  So take the current date, subtract a week's worth of seconds off it (that's where the 604800 comes from) and then compare it to the published date (also U date formatted).  However, no dice.  Doesn't seem to like the subtraction...or maybe it's the replace_keywords doesn't work in v4.12.


(Anewport) #4

Hey Tim I like your solution a lot more. Pretty sure the reason it isn't working for you is due to system versions again as global keywords don't work with the replace_keywords modifier until 4.16.1 too.

 

Have a look at http://manuals.matrix.squizsuite.net/patches/4.16.1 "Minor Enhancement #6407: Global Keyword Replacement Support on the replace_keywords modifier."


(Tim Davison) #5

Awesome.  So...I answered my own question with a solution I can't use.  I can taste the irony.


(Anewport) #6

I know the feeling all too well haha


(Tim Davison) #7

Update: Probably not related to Matrix, but anyone going along this path may find the same issue.  

 

I went the javascript path to determine date differences, comparing the current date to the meta DC.data.modified tag.  The built-in javascript Date.parse() function does not recognise ISO8601 date formats in IE8 (not sure about other version of IE).  FF and Cr recognised and parsed them fine.  

 

Point is, if you are using IE8 and ISO8601 formats you will need to get an external library (or write the parse code yourself) - you will not be able to rely on the built-in.  Or else render the date in a format supported in IE8.