如果我有一个日期,我如何计算该日期在那一年中的周数?
If I have a date, how do I calculate the week number for that date within that year?
例如,在 2008 年,1 月 1 日至 1 月 6 日在第 1 周,而 1 月 7 日至 13 日在第 2 周,因此如果我的日期是 2008 年 1 月 10 日,则我的周数为 2.
For example, in 2008, January 1st to January 6th are in week 1 and January 7th to the 13th are in week 2, so if my date was January 10th 2008, my week number would be 2.
算法非常适合让我入门,示例代码也会有所帮助 - 我正在 Windows 上使用 C++ 进行开发.
An algorithm would be great to get me started and sample code would also help - I'm developing in C++ on Windows.
从日期中获取周数MS SQL Server 2005?
伪代码:
int julian = getDayOfYear(myDate) // Jan 1 = 1, Jan 2 = 2, etc...
int dow = getDayOfWeek(myDate) // Sun = 0, Mon = 1, etc...
int dowJan1 = getDayOfWeek("1/1/" + thisYear) // find out first of year's day
// int badWeekNum = (julian / 7) + 1 // Get our week# (wrong! Don't use this)
int weekNum = ((julian + 6) / 7) // probably better. CHECK THIS LINE. (See comments.)
if (dow < dowJan1) // adjust for being after Saturday of week #1
++weekNum;
return (weekNum)
澄清一下,这个算法假设你像这样计算你的周数:
To clarify, this algorithm assumes you number your weeks like this:
S M T W R F S
1 2 3 <-- week #1
4 5 6 7 8 9 10 <-- week #2
[etc.]
getDayOfWeek() 和 getDayOfYear() 是大多数语言中的标准日期对象操作.如果您的没有它们,您可以从某个已知日期(1970 年 1 月 1 日是常见的日期)开始计算,然后查看一周中的哪一天.
getDayOfWeek() and getDayOfYear() are standard date-object operations in most languages. If yours doesn't have them, you can count-forward from some known date (Jan 1, 1970 is a common one), after looking up to see what day of the week it was.
如果您要实现自己的日期计数例程,请记住可被 100 整除的年份是不是闰年,除非它们也可被 400 整除.因此 1900 不是闰年年,但 2000 年是.如果您要回到过去工作很长时间,则必须弄乱公历与儒略历等,请参阅 维基百科提供大量相关信息.
If you're going to implement your own date counting routines, remember that years that are divisible by 100 are NOT leap years, unless they are also divisible by 400. So 1900 was not a leap year, but 2000 was. If you're going to work far back in time, you have to mess with Gregorian vs Julian calendars, etc., see Wikipedia for loads of info on that.
此链接 更详细地讨论了 Windows/C++ 中的日期/时间函数细节.
This link talks about date/time functions in Windows/C++ in greater detail.
这篇关于如何计算给定日期的周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!