Date and Time Subtraction


Is there a method that takes two dates and returns the number of days difference.

I have a work order number that I want to know how long it has been there… Initially I plan to use timer, but thinking if I could just subtract the work order time stamp from current time stamp.


Did you see this forum post here;


PAC control but Node-Red looks like an alternative.


Be sure to read the whole thread that Beno linked to, there are some bugs in there. What format is your time stamp in?


See below for given data (each column stored on PAC Table):

item 1, 04/20/2019, <elapse time in days here>
item 2, 04/22/2019, <elapse time in days here>

item 10, 04/25/2019, <elapse time in days here>

The third column is the table for elapse time.
I am trying to come up with a code, to loop thru each row.
Subtract 2nd Column (Date stamp), from Current Date and save it on corresponding index of 3rd column. Every 1 min or so.
Number of Items can be much more.


Is each column is stored in a separate table? Are they all string tables? Are you comfortable with string parsing?


Well, PAC can only store 1 dimension table, and 1 data type. So yes, it is separate table.
Yes, first and 2nd column is string.
Yes, I can parse.
Do you have something like this in mind:
function (subroutine in this case) that takes two argument mm/dd/yyyy, and returns an interger.
Is this essentially what Mary’s codes does?

Thank you.


Yes, I’m aware, just verifying that it wasn’t a comma separated string in a single table.

I would parse out the month, day and year and put them into an integer table in the date/time format as specified in the PAC Control command help for the GetDateTime command. Convert this value to an int64 NTP timestamp using the DateTimeToNtpTimestamp command. Do the same with the current time. Subtract the two int64 values that the Ntp command generates and divide the result by the constant (2^32 * 86400 = 371,085,174,374,400). That should get you the difference in days.

If you can store the NTP timestamp when the work order is generated in an int64 table, that would make things simpler.


Thank you.
Sounds like the best plan.
I will develop this, and update this thread.



@Beno Let me take this opportunity to ask.

About the solution Mary coded and Philip advised, can Codesys do these things?

How about writing to a txt file. PAC does it, can Codesys do it too?


@philip I cannot find command that gets NTP timestamp directly.
Do we usually do two steps for this: GetDateTime and DateTimeToNtpTimestamp?


Yes that is correct.