Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use gmtime or localtime to calculate days since Sunday (that is, day of the week) and days since January 1 (note that Jan 1 is "0" in the latter).</p> <p>The arbitrary bit is deciding what day of the year Week 1 starts on: usually it only depends what day of the week Jan 1 was, which of course you can calculate from the two pieces of information from gmtime. Then use a table lookup for the 7 possibilities, it's probably easier than coding the rules.</p> <p>For example, I think Outlook uses the standard that Week 1 is the first week containing a Thursday. So if Jan 1 is a Sunday, then the first day of Week 1 is Jan 1, or day 0. The remaining possibilities are Monday, -1; Tuesday, -2; Wednesday, -3; Thursday, -4; Friday, 2; Saturday, 1.</p> <p>Note the negative numbers: "Sunday of week 1" doesn't actually exist in 4 out of 7 cases, but if we pretend it was a day back in the previous year, we'll get the right answer out.</p> <p>Once you have that, the number of days between it and your date tells you the week number: divide by 7 and add 1.</p> <p>That said, I imagine there's a Windows API somewhere that will give you the same week number that Outlook uses. I just don't know what it is, and of course if your Week 1 rules are different from Outlook's then it's probably not much use.</p> <p>Untested code:</p> <pre><code>int firstdays[7] = { 0, -1, -2, -3, -4, 2, 1 }; // or some other Week 1 rule struct tm breakdown; time_t target = time_you_care_about(); _gmtime_s(&amp;breakdown,&amp;target); int dayofweek = breakdown.tm_wday; int dayofyear = breakdown.tm_yday; int jan1wday = (dayofweek - dayofyear) % 7; if (jan1wday &lt; 0) jan1wday += 7; int week1first = firstdays[jan1wday]; if (dayofyear &lt; week1first) return 0; return ((dayofyear - week1first)/7) + 1; </code></pre> <p>Something like that, anyway.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload