Sign in to follow this  
Followers 0
Paulo Ferreira

Calculating number of days between two dates

11 posts in this topic

Hello there. Basically the title says it all. I'm using a CJ2M-CPU11. I have 2 different dates that I got through A352 and A353 (day, month and year) and I need to find the number of days between the 2 dates, do you know of any simple way to get it? Is there an instruction to convert the date or to get the date from the system in seconds with reference to some epoch? Didn't find what I needed in the manual and now I'm venturing with algorithms for calculating julian days.

Share this post


Link to post
Share on other sites
CJ2 Clock Instructions Reference Manual .pdf

Share this post


Link to post
Share on other sites
Not really following you Jay. CADD and CSUB add or subtract hours, minutes and seconds to a date, I don't see how I can use it when I have 2 dates (year, month, day). Can you give me a hint on what you were thinking, some pseudo-algorithm so I can elaborate on that. Thanks.

Share this post


Link to post
Share on other sites
Good solution! What about power off over midnight?

Share this post


Link to post
Share on other sites
What about power off over midnight? The logic should work if power is turned off one day and turned on the next day since DM is retentive. What won't work is if the power is turned off for more than 1 day since the day counter increment only once when DM0 <> DM1. HMMMMM!! Have to give that some more thought.

Share this post


Link to post
Share on other sites
That seems really simple and effective if the power is always on, or at least powered every day. However the machine can be powered off for several days, and when turned on I need to know how many days have passed since the last recorded date. I've just finished my solution, implemented the julian date algorithm, it's somewhat more complex. Created a ST Function Block that calculates the Julian Number of each gregorian date, and then subtracts the second Julian day from the first and the result is the number of days. Check it out in the attachment if you want. I have just a small problem, the conversion functions REAL_TO_UINT or REAL_TO_DINT round the number to the nearest integer but I needed them to be truncated. Can't seem to find a standard ST function to do that. Bytes, if you figure out a easier way to do this, please advise me ;) EDIT: Well... went with the classic subtract 0.5 and let it round to the nearest integer and it works great, takes into account leap years and everything. Updated the attachment if you guys want to see it and comment on how ugly it is. :) Edited by Paulo Ferreira

Share this post


Link to post
Share on other sites
Nice Solution One trick that I use to remove rounding is to subtract 0.5 from the real then let it round

Share this post


Link to post
Share on other sites
Grr - my timing wasn't so good Just editing my post added it here anyway Someone want to expand on this pseudo code idea into something that works? Limitations - Depends on the number of years in the YearArray Calculate the number of Days from 1/1/2011 for each Date then subtract To Calculate the number of days from 1/1/2011 Have an array with day of year for the month for a Normal Year Array[0] = 0 // Days for January Array[1] = 31 // Feb Array[2] = 59 // March ... Array[11] = 334 // Dec DayOfYear = Array[Month -1] + Day IF a Leap Year AND Month > Feb Then add 1 to the DayOfYear YearArray[0] = 0 // for year 2011 YearArray[1] = 365 // For Year 2012 Leap Year YearArray[2] = 731 // For start of year 2013 YearArray[3] = 1096 // For Start of year 2014 YearArray[4] = 1461 DaysSince2011 = DayOfYear + YearArray[Year - 2011] The YearArray you can fill on power up using the previous year so the base Date can keep on incrementing Test for Leap Year is If (Year Mod 4 = 0 AND Year Mod 100 <> 0) OR Year mod 400 = 0 THEN Leap Year Edited by michael G

Share this post


Link to post
Share on other sites
The logic seems correct and simple. I believe it would work just fine... well, unless you disconnect the machine and connect it after a couple of billion years which seems unlikely :) I'll probably give that algorithm a go, no complex real number divisions. Thanks Michael.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0