C Language Development Kit/Snippets/Miscellaneous/WhatDayOfWeek

From OptiWiki

Jump to: navigation, search
Description Return an integer value representing the current day of the week. Starting with Sunday as 0.
Syntax
int WhatDayOfWeek( void );
Arguments None.
Returns Integer value representing the current day of the week.
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
Remarks None.
Example
#include "lib.h"
#include <stdio.h>
 
//
// Array with the names of the days of the week
//
const char day[][9+1] =
{
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};
 
// Returns the current day of the week
// 0 = sunday, 1 = monday, 2 = tuesday, 3 = wednesday
// 4 = thursday, 5 = friday, 6 = saturday
int WhatDayOfWeek( void )
{
    int m, y, a;
    struct date curr_date;
 
    getdate( &curr_date );
    a = (14 - curr_date.da_mon ) / 12;
    y = curr_date.da_year - a;
    m = curr_date.da_mon + (12*a) - 2;
 
    return((curr_date.da_day + y + (y/4) - (y/100) + (y/400) + ((31*m)/12))%7 );
}
 
void main( void )
{
    for(;;)
    {
        printf("\f%s", (char*)day[ WhatDayOfWeek() ] );
        delay( 250 ); // wait 5 seconds before update
    }
}


Views
Personal tools