TIA Portal v18: How can I ADD 6 months to a date?

Mas01

Member
Join Date
Oct 2020
Location
Leicester, England
Posts
1,109
I'm getting stuck again.

Operator enters date when calibration was performed.

I want to ADD 6 months onto that date - this will be the due date for the next calibration.

How can I add 6 months in ladder logic?

See picture for data block declaration of tags:

cal date.png
 
Here's a function to do the job - if you want it in ladder then you will have to convert it yourself. NB tested in a S7-1500 plc.



Code:
FUNCTION "fc7_AddSixMonths" : Date
{ S7_Optimized_Access := 'FALSE' }
VERSION : 0.1
   VAR_INPUT 
      dtLast : Date;
   END_VAR

   VAR_TEMP 
      DTL1 {InstructionName := 'DTL'; LibVersion := '1.0'} : DTL;
      myDTL1 AT DTL1 : "myDTL";
      DTL2 {InstructionName := 'DTL'; LibVersion := '1.0'} : DTL;
      myDTL2 AT DTL2 : "myDTL";
   END_VAR


BEGIN
    #DTL1 := DATE_TO_DTL(IN := #dtLast);
    #DTL2 := #DTL1;
    
    #myDTL2.usMonth := #myDTL1.usMonth + 6;
    IF #myDTL2.usMonth > 12 THEN
        #myDTL2.usMonth := #myDTL2.usMonth - 12;
        #myDTL2.uiYear += 1;
    END_IF;
    
    #fc7_AddSixMonths := DTL_TO_DATE(IN := #DTL2);
END_FUNCTION
 
UDT:


Code:
TYPE "myDTL"
VERSION : 0.1
   STRUCT
      uiYear : UInt;
      usMonth : USInt;
      usDay : USInt;
      usWeekDay : USInt;
      usHour : USInt;
      usMinute : USInt;
      usSecond : USInt;
      udNanoSecond : UDInt;
   END_STRUCT;

END_TYPE
 
Two approaches: the one-instruction approach from the previous post; another using DTL, if you want to keep the same day-of-month less than 29.

Both handle leap-days in their own way: with the simple approach it is built-in; the complex approach simple disallows leap-days in the result.

You declare the variables as shown (DATE, DTL, USINT) in the DB window.
Untitled.png
 
Honestly, calendar math is super hard. There are so many corner cases: leap years (except the ones that aren't), months with different numbers of days...

I think drbitboy's ideas are solid. Is 6 months-ish good enough? If so, I'd just add 30 days * 6 mo = 180 days (or the 182 or 183 from above) and move on. If not.... check for as many corner cases as you can think of. DTL makes it easy to parse the date/time, but unfortunately it isn't magic, and the PLC doesn't automatically shift everything if the month suddenly becomes 14. It just fails when you try to use the DTL later.
 
Honestly, calendar math is super hard. There are so many corner cases: leap years (except the ones that aren't), months with different numbers of days...

I think drbitboy's ideas are solid. Is 6 months-ish good enough? If so, I'd just add 30 days * 6 mo = 180 days (or the 182 or 183 from above) and move on. If not.... check for as many corner cases as you can think of. DTL makes it easy to parse the date/time, but unfortunately it isn't magic, and the PLC doesn't automatically shift everything if the month suddenly becomes 14. It just fails when you try to use the DTL later.

Agreed, that's a very pragmatic approach!
 
Also oscat basic (S7) free library should have date_add function.



(haven't never tested it)



http://www.oscat.de/de/component/jdownloads/category/2-oscat-basic.html?Itemid=0

http://www.oscat.de/de/component/jdownloads/send/2-oscat-basic/9-oscat-basic333-en.html?Itemid=0






The function DATE_ADD add days, weeks, months, and add years to a
date. First the module adds the specifed days and weeks, then months
and fnally the years.
The input values can be both positive as well be negative. So it can also
be subtracted from a date.
Note that especially for negative input values the sum of negative values,
i.e. -3000 days does not run below 1.1.1970 because this would have an
overfow of data type DATE and undefned values are obtained.


Example: DATE_ADD(1.1.2007,3,1,-1,-2) = 11/12/2005
adds additional 3 days and 1 week and then draws 1 month
and 2 years from.
 

Similar Topics

hello every one. i'm new to tiaportal, i have created new project and HMI screen the program works fine with PLC-sim, but when i try to cntrol the...
Replies
9
Views
434
My PLC (S7-1200) and HMI (KTP-1200 Basic) has been delivered on-site to the customer. To be able to do "off-line" updates to the code, I am using...
Replies
4
Views
229
hi everyone. i hope you guys are doing great. i am trying to built communication between aveva intouch hmi 2023 and tia portal v18. i dont have...
Replies
1
Views
580
Hi, My PLC hardware has been delivered to customer site - many thanks for the invaluable help on this forum! I was looking into see if it's...
Replies
12
Views
2,548
Tia portal v18 issue,online icon greyed out but plc comms must be established as.blocks showing and no offline copy on laptop.
Replies
7
Views
1,121
Back
Top Bottom