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;

1 Like

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.

I am new to creating subroutine, is it possible to create subroutine, to call it and return current int64 NTP_TimeStamp data. This way, I do not have to use an Integer32_Table on my current strategy.

Yes, you can call GetDateTime in a subroutine using a local int32 table, and then pass that to the DateTimeToNtpTimestamp to get the int64 timestamp.

A subroutine doesn’t “return” anything, it can only act on the parameters passed to it, in your case you would pass in the int64 variable that you want the timestamp to be stored in.

Thank you philip, you are faster that support. :slight_smile:

I have 9 running charts that I want to get NTP_TimeStamp.
So I need to create 9 integer_table with 8 elements. and 9 integer64 variables.
I would run GetDateTime and followed by DateTimeToNtpTimestamp, on each charts.

I plan to use subroutine, so that, I save myself creating 9 integer_table.
Believing that, calling a subroutine will create its own instance.
But I learned that calling the same subroutine simultaneously is not a good practice.

Sounds reasonable.

:roll_eyes: Who told you that?

Doesn’t matter who told me, any experience on this?
Realizing what you said means, main purpose of subroutine will be defeated.
And it is also state on manual, that subroutine will be displayed on the command list, just like other commands - in that context you can treat subroutine like other commands…
(I now believe this is the case. And tested. Works just fine. :slight_smile: )

Cool. :sunglasses: Thank you.

Yes, you can call them from multiple charts as much as you like. Works fine, do it all the time.

1 Like

NTP Timestamp returning Negative value?