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"
-
Show us what you've tried and we'll do our best to help you. – Sanguinary Oct 15 '18 at 09:02
-
1Could a function called `GetDayOfWeek ` from CAA DTUtil library help you? Haven't tried it myself so thats why just commenting. [Link to reference](https://help.codesys.com/webapp/OagqIKs-TqmhZlQwlX8bdNv_ho8%2FGetDayOfWeek;product=CAA_DTUtil_Extern;version=3.5.12.0) – Quirzo Oct 15 '18 at 13:00
-
1The library OSCAT [Open Source Community for Automation Technology](http://www.oscat.de/) includes a function “DAY_OF_WEEK”. – dergroncki Oct 16 '18 at 05:48
-
Thank you both, i will have a look at those. I have previously read about dayOfWeek function in Syslibtime library, but understood it was only used for system time – Ana Oct 17 '18 at 05:49
-
If you use SysLibTime you get variable that already have property day of week. – Sergey Romanov Oct 19 '18 at 13:27
2 Answers
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.

- 849
- 6
- 19
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
- Internal time storage is in seconds from 1 Jan. 1970.
- We know that 1 January 1970 was a Thursday.
- Ther are 86400 seconds in one day
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);

- 2,949
- 4
- 23
- 38