获取一个月的周数 [英] Get the number of weeks for a month

查看:119
本文介绍了获取一个月的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得一个月的周数?因此,我希望得到整个星期,即使是本月不完整的星期。

How can I get the number of weeks for a month? Thereby I want to get all weeks, even the one which are not full weeks for this month.

推荐答案

30个月的月份有4个2/7周的价值天,但那些日子可能不会开始或结束一整周。你已经知道这是问题的本质。



如果一个月的第一天与一周开始对齐 - 你知道这个月有5个星期 - 整整四个星期和'备用天'开始第五周。对于30天的月份,只要它在一周的第一天和第六天之间的任何地方开始,这将是真的。



如果该月的第一天排队然而,在一周中的第七天,你进入六周模式。



对于31天的月份,最后一个案例包括第六个和一周的第七天。



每月28或29个月 - 我相信你可以自己弄清楚这种模式。



所以 - 我会创建四个查找表(一个月一个28,29,30,31天),每个有七个条目。该条目包含一个月内的天数;您可以根据当月第一个工作日索引到表中。
A 30 day month has 4 2/7 weeks' worth of days, but those days may not start or end on a whole week. This you already know it being the nature of your problem.

If the first day of the month lines up with the beginning of a week - you know the month covers 5 weeks - four whole weeks and the 'spare days' starting the fifth week. For a 30 day month, provided it starts anywhere between the first and the sixth day of the week this will be true.

If the first day of the month lines up with the SEVENTH day of the week, however, you're into the 'six week' mode.

For a 31 day month, that last case includes the sixth and seventh day of the week.

For a 28 or 29 day month - I'm sure you can figure out the pattern yourself.

So - I would create four lookup tables (one each for 28,29,30,31 days in a month), each with seven entries. The entry contains the number of days in a month; you index into the table based on the weekday of the first on the month.


MSDN样式:

MSDN Style:
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;

var currentWeekno = cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);



因此,创建月份日的第一天和月份的最后一天,并且您有周数。


So create a first day of month date and a last day of month date and you have the weeknumbers.


这篇关于获取一个月的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆