Reputation: 97
very new to Codesys so bear with me. I know you can use a time picker, but it doesn't get displayed on the web visualisation for some reason. So trying to find a function that will display the day of the week that corresponds to the chosen date. eg. select 15.10.2018 and get "Monday"
Upvotes: 1
Views: 2437
Reputation: 3080
It depends on what you have as an input. If you have month, day and year as separate INT
values the above example might work. But you can also convert it to DATE
which is much better format to work with. That will alow you quickly convert to TIME
or TOD
and compare dates and do much more.
VAR
DT: DATE;
Str: STRING;
d : INT:= 15; //day
m : INT:= 10; //month
y: INT:= 2018; //year
END_VAR
Str := CONCAT("D#", y);
Str := CONCAT(Str, '-');
Str := CONCAT(Str, m);
Str := CONCAT(Str, '-');
Str := CONCAT(Str, d);
(* Now our string is D#2018-10-15 *)
DT := STRING_TO_DATE(Str);
If you have type DATE
, then to calculate day of thr week is very trivial task. All we need to know is what was the day of the week in any given day. Then we can calculate how many days we are from that day, devide by 7 and get MOD.
Here are the facts we have to know
Here is a function example.
FUNCTION WeekDay : UINT
VAR_INPUT
DT: DATE;
END_VAR
VAR
NumOfDays: DWORD;
END_VAR
(* How many days from 1 Jan. 1970 *)
NumOfDays := DATE_TO_DWORD(DT) / 86400;
WeekDay := DWORD_TO_UINT((NumOfDays + 3) MOD 7);
END_FUNCTION
+3 give us 0 - Monday because in system where 0 is Monday 3 is Thursday and if we want 0 - Sunday we can use +4;
Of course you can optimize function to be only one line
WeekDay := DWORD_TO_UINT(((DATE_TO_DWORD(DT) / 86400) + 3) MOD 7);
Upvotes: 1
Reputation: 849
there is a formula for calculating the day of the week on Wikipedia (German).
In CoDeSys:
PROGRAM PLC_PRG
VAR
d : INT:= 15; //day
m : INT:= 10; //month
y: INT:= 2018; //year
w: DINT; //result -> day of the week 1 = monday ...
END_VAR
Implementation:
w:= ((d + TRUNC(2.6 * ((m + 9) MOD 12 + 1) - 0.2) + y MOD 100 +
TRUNC(y MOD 100 / 4) + TRUNC(y / 400) - 2 * TRUNC(y / 100) - 1) MOD 7
+ 7) MOD 7 + 1;
This returns the day of the week as number. 1 is Monday, 2 is Tuesday etc.
Upvotes: 2