Search code examples
timepickercodesysst

generating week day by knowing the date in Codesys


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"


Solution

  • 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.