Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Resolving date difference on Apache NIFI

avatar
New Contributor

Good afternoon all, and thank you in advance.

I have a Apache NIFI solution that is pulling from API a pair of dates, i've grabbed out the relevant bit from the json:

"arrival" : "2023-07-24T14:33:00.263Z", "departure" : "2023-07-24T14:21:15.263Z"

And i'm successfully getting those values down to attributes via extract attributes.

However when i perform a toNumber() on them to convert to miliseconds since EPOCH to determine the time between the dates, i return an empty condition upon review of my flow in the queue.

I've gone so far as to arbitarily grab "NOW()" and attempt to toNumber() it but with the same result as trying to modify the above attributes.  incidentally NOW() returns a format like: Mon Jul 24 15:39:21 UTC 2023 - its different that my UTC formatted items, but it also doesn't want to toNumber() - returning again an empty value


Screenshot 2023-07-24 133844.png
Shows me attempting to convert the attributes:
image
and this shows the flow in the queue after hitting that updateattrs.  The datetimestamps are all there, but the toNumber() fails for both format styles...  timenow was set earlier with a simple ${now()}, and none of them are formatted.  when attempting to format them, actually, i get other errors.


I'm struggling to work with dates in nifi and hoping someone has some guidance for me.  

NOTE: yes, my nifi is reskinned - its easier on the eyes.

1 ACCEPTED SOLUTION

avatar

Hi @maglinvinn ,

 

To get the expected result, you need to convert the string type datetime to datetime type before calling the toNumber() function. The conversion can be done using the toDate() function  (see: https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#todate ), For example the arrivaltimeinmillis will have the following value:

 

${timearrival:toDate("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"):toNumber()}

 

If you find this is helpful, please accept solution.

Thanks

View solution in original post

2 REPLIES 2

avatar

Hi @maglinvinn ,

 

To get the expected result, you need to convert the string type datetime to datetime type before calling the toNumber() function. The conversion can be done using the toDate() function  (see: https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#todate ), For example the arrivaltimeinmillis will have the following value:

 

${timearrival:toDate("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"):toNumber()}

 

If you find this is helpful, please accept solution.

Thanks

avatar
New Contributor

thank you so much! i'm moving forward again!