I have the current week as an integer (43 as of now).
I need the date for Monday in a format like ‘Mon Oct 25’.
Thought I could accomplish that by a function from but I don’t know how to do that.
Any suggestions?
EDIT:
I tried the suggestion from R., but it doesn’t give the expected result. Did I implement it wrong?
time_t monday;
char date_format[32];
time_t now = time(NULL);
struct tm *tm = localtime(&now);
tm->tm_yday = 0; // reset to Jan 1st
tm->tm_hour = 24 * 7 * WEEK + 24; // goto Sun and add 24h for Mon
monday = mktime(tm);
strftime(date_format, 31, "%a : %D", tm);
printf("%s\n", date_format);
Note: Not tested, but given the current year, this should do it:
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
ctimefunction, which just so happens to display more than you asked for. You would pass to it a pointer to thesecsSinceEpochvalue and truncate its output to just display the day of the week, the day of the month and the abbreviation of the months name.