Followers 0

# Converting DATE_AND_TIME variable value to year, month, day, etc

## 3 posts in this topic

Hello guys: I would like to get an way to get the year, month, day, hour, minute, and seccond in different variables, coming from a value on a DATE_AND_TIME variable. For example, using an FB like the following one: Where the IN and Output data of this FB is like this: FUNCTION_BLOCK DT_TO_year_month_day_hour_and_minute VAR_INPUT In_DT: DATE_AND_TIME; END_VAR VAR_OUTPUT Year: UINT; (*value from 1970 to 2106*) Month: USINT; (*value from 1 to 12*) Day: USINT; (*value from 1 to 31*) Hour: USINT; (*value from 0 to 23*) minute: USINT; (*value from 0 to 59*) secconds: USINT; (*value from 0 to 50*) END_VAR VAR END_VAR However, I only have the variables declaration. The programming code to do this seems very complicated for me. Does anybody have this code already done? Or, does anybody known an easiest way to do this? Thank you very much. Marco

##### Share on other sites
Well, all the "DT" variable is is the number of seconds since 1970. This means by using simple division you should be able to come up with each one of your variable outputs. DT / number of seconds in a year = Number of years Then you take the remainder of that division and divide it to get months MOD(DT / Number Seconds in a year) / Number Seconds in a month = Number of Months And so on.... Since there would be quite a few calculations that won't change most of the day, depending on what hardware platform you're using you may want to use a simplified timer to make sure you don't recalculate the year/month/date every single scan of the PLC. Hope it helps! Nick