Hello Community!
I’m trying to fix a small issue on a website and it’s driving me crazy, hope someone here can help me!
I’m trying to print in an HTML file a date from a HubDB row and it’s returned as a timestamp, for example: 1635984000000
I would like this to have a proper dd/mm/yyyy format (or similar), so I’m using the following code:
{{ row.date|datetimeformat('%B %e, %Y') }}
Where row.date refers to the date element from HubDB.
The issue is that I get the following error:
com.hubspot.jinjava.interpret.InterpretException: Input to function must be a date object, was: class java.lang.String
This makes me think that the solution is adding first the following filter:
|strtotime("yyyy-MM-dd'T'HH:mm:ssZ")
So it will look like this:
{{ row.date|strtotime("yyyy-MM-dd'T'HH:mm:ssZ")|datetimeformat("%B %e, %Y") }}
But to my surprise, the following error appears:
stringToTime() could not match datetime input with datetime format yyyy-MM-dd'T'HH:mm:ssZ
I took all this filters from the documentation.
Does anyone know what am I doing wrong?
Thank you in advance!
Hello @albertsg and thank you for writing,
Probably there is nothing wrong with what you’re doing, seems correct, the data from the row.data seems to be the issue, the returning value should be a number representing the timestamp, the output complains that the value is a string, I think you should do {{ row.date|pprint }} so we can debug it.
@albertsg If you’re using a date field that definitely should work. You could also try the function instead of the filter to see if that makes any difference :
{{ datetimeformat(row.date,'%B %e, %Y') }}
Hi @miljkovicmisa and @alyssamwilie thanks for the replies! I managed to find the solution, it was not exactly what you suggested but your replies definitely guided me in the correct way, so thank you again!
Here’s what happened:
This was the output after the pprint: (Long: 1635984000000)
It made me think that I needed to convert it to string in order to be able to use strtotime, but it didn’t work even if I did the following:
{{row.date|string|strtotime("yyyy-MM-dd'T'HH:mm:ssZ")|datetimeformat("%B %e, %Y")}}
Then I tried @alyssamwilie’s solution by using the function but it also didn’t work, I got this error (in long and string format):
Error invoking function 'datetimeformat'
At this point it was quite clear that what I needed was to convert this “long” variable to a proper timestamp format. I tried using to_local_time and it worked!
So at the end this is what I used to make it work:
{{to_local_time(row.date)|datetimeformat("%B %e, %Y")}}
It’s weird to me that I cannot apply the filter directly in the data obtained from HubDB, but I’m happy we managed to fix it.
Thank you again for your help!
For anyone who stumbles on this. datetimeformat was deprecated and replaced with format_datetime