In Javascript you can use this:
getWeekDay = function (year) {
var d = new Date();
d.setFullYear(year,0,1);
return d.getDay()+1;
};
document.write(getWeekDay(2011));
Result is 1..7, as requested.
Answer from phlogratos on Stack Overflowtime - How to get first day of week in c - Stack Overflow
dayofweek - Get the first day of week of a year (any programming language) - Stack Overflow
c - Calculate first day of a calendar week - Stack Overflow
C Program to find day of week given date - Stack Overflow
As reported also by Wikipedia, in 1990 Michael Keith and Tom Craver published an expression to minimise the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week.
The expression does preserve neither y nor d, and returns a zero-based index representing the day, starting with Sunday, i.e. if the day is Monday the expression returns 1.
A code example which uses the expression follows:
int d = 15 ; //Day 1-31
int m = 5 ; //Month 1-12`
int y = 2013 ; //Year 2013`
int weekday = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;
The expression uses the comma operator, as discussed in this answer.
Enjoy! ;-)
A one-liner is unlikely, but the strptime function can be used to parse your date format and the struct tm argument can be queried for its tm_wday member on systems that modify those fields automatically (e.g. some glibc implementations).
int get_weekday(char * str) {
struct tm tm;
memset((void *) &tm, 0, sizeof(tm));
if (strptime(str, "%d-%m-%Y", &tm) != NULL) {
time_t t = mktime(&tm);
if (t >= 0) {
return localtime(&t)->tm_wday; // Sunday=0, Monday=1, etc.
}
}
return -1;
}
Or you could encode these rules to do some arithmetic in a really long single line:
- 1 Jan 1900 was a Monday.
- Thirty days has September, April, June and November; all the rest have thirty-one, saving February alone, which has twenty-eight, rain or shine, and on leap years, twenty-nine.
- A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
EDIT: note that this solution only works for dates after the UNIX epoch (1970-01-01T00:00:00Z).
Note: Not tested, but given the current year, this should do it:
const char *months[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
"Oct","Nov","dec","Jan"};
/* Start with January 1st of the current year */
struct tm curYear={
0, // secs
0, // mins
0, // hours
1, // Day of month
0, // Month (Jan)
year,
0, // wday
0, // yday
0}; // isdst
/* Offset the number of weeks specified */
time_t secsSinceEpoch=mktime(&curYear)+
weekNum*86400*7; /* Shift by number of weeks */
struct tm *candidateDate=gmtime(&secsSinceEpoch);
/* If the candidate date is not a Monday, shift it so that it is */
if (candidateDate->tm_wday!=1)
{
secsSinceEpoch+=(86400*(candidateDate->tm_wday-1));
candidateDate=gmtime(&secsSinceEpoch);
}
printf("Mon %s %d",months[candidateDate->tm_mon],candidateDate->tm_mday\n");
You may have to adjust the formulas in this code depending on what exactly you mean by week 43 of a given year or to conform with ISO-8601, for example. However, this should present you with good boiler plate code to get started. You may also want to parameterize the day of the week, so that it is not hard coded.
Also, if you want, you can avoid the months array and having to format the time, by truncating the result of the ctime function, which just so happens to display more than you asked for. You would pass to it a pointer to the secsSinceEpoch value and truncate its output to just display the day of the week, the day of the month and the abbreviation of the months name.
The mktime function can do this. Simply initialize struct tm foo to represent the first day of the year (or first day of the first week of the year, as needed), then set tm_hour to 24*7*weeknum and call mktime. It will normalize the date for you.

