Is there an Arduino library that can take in a DD/MM/YYYY date and tell me what day of the week it is?

587 views Asked by At

I am making a Clock and I have got all the hardware figured out and I'm working on the software now. I have the exact date of a particular day, but I want the Arduino to be able to convert that into a day of the week. I have had a look online, but I can't seem to get any of the libraries to work. Does anyone have any easy to use code for this problem?

1

There are 1 answers

1
David Ranieri On

You don't need a library for that, you can use Tomohiko Sakamoto's Algorithm based on offsets:

/**
 * Sunday = 0 ... Saturday = 6
 */
int day_of_week(int day, int month, int year)
{
    static const int offset[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

    year -= month < 3;
    return (year + year / 4 - year / 100 + year / 400 + offset[month - 1] + day) % 7;
}

or if you prefer:

/**
 * ISO 8601 date and time standard
 * Monday = 1 ... Sunday = 7
 */
int ISO_day_of_week(int day, int month, int year)
{
    static const int offset[] = {6, 2, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3};

    year -= month < 3;
    return (year + year / 4 - year / 100 + year / 400 + offset[month - 1] + day) % 7 + 1;
}