Has anyone built a function block for getting the day of the week? I remember there used to be a function in the AsDateTime library but I can no longer find that library in AS6. Thanks!
Hi,
have you had a look into the function block DTStructureGetTime?
This function blocks reads the actual date and time into a structure of type DTstrucure, which contains a element .wday that represents the day of week.
If you need this information not for the actual date and time, you could set a DATE_AND_TIME variable to the value needed, and convert it using DT_TO_DTstructure.
Best regards!
Ahhh that works! Thank you.
Hello CJ and welcome to the B&R Community page!
Please do let us all know if the question has been answered for future easy reference.
Cheers,
Eren
#define IS_LEAP_YEAR(yr) (!((yr)%400) || (!((yr)%4) && (((yr)%100))) )
char *ascii_days={“Sun”,“Mon”,“Tue”,“Wed”,“Thu”,“Fri”,“Sat”};
int weekday(long year, long month, long date)
{
long days, wkday, num_lp_yrs;
int mon;
int month_days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
/* Notice we say, "year-1" because this parameter year is not complete
* and hence not counted. We will count the days of the current year later. */
num_lp_yrs = (year-1)/4; /* How many leap years? One every 4 yrs, but...*/
num_lp_yrs -= (year-1)/100; /*...centennials are not leap years...*/
num_lp_yrs += (year-1)/400; /*...unless that centennial is a multiple of 400*/
days = (year-1)*365 + num_lp_yrs; /*Capture total number of days*/
/* If this is a leap year make February 29 days */
if(IS_LEAP_YEAR(year))
month_days[1] = 29;
/* Now, let's get the days spent of the parameter/'current' (incomplete) year
* by examining month and day of the year....because our days count (so far) is
* only up to January 1st of the parameter year. Hence, we must add the
* days of the current year (since Jan 1st) to the days variable. Parse
* and sum every applicable month of the array... */
for(mon = 0; mon < month-1 && mon < 12; mon++)
days += month_days[mon];
/* Finally, add in the parameter date given. */
days += date;
/* 7 days in a week, so... modulo 7 nicely tells the weekday*/
wkday = days%7;
return (int)wkday;
}
with regards