使用Visual Basic .NET,如何查找特定年份和月份的星期六和星期日的数量?特定年份和月份的星期六和星期日的数量
0
A
回答
1
我已经创建了一个使用计算方法来计算SATS和周日的功能。我还没有对它进行过测试。我已经在@ p.campbell的答案中测试了两者的性能以及计算方法(下面)和迭代方法,10,000次调用的结果以毫秒为单位。
计算:7 迭代:39
希望有所帮助。
戴夫
Dim month As Integer = 12
Dim year As Integer = 2011
'Calculate the Start and end of the month
Dim current As New DateTime(year, month, 1)
Dim ending As DateTime = current.AddMonths(1)
'Ints to hold the results
Dim numSat As Integer = 0
Dim numSun As Integer = 0
'Numbers used in the calculation
Dim dateDiff As Integer = (ending.Date - current.Date).Days
Dim firstDay As DayOfWeek = current.DayOfWeek
'Figure out how many whole weeks are in the month, there must be a Sat and Sunday in each
' NOTE this is integer devision
numSat = dateDiff/7
numSun = dateDiff/7
'Calculate using the day of the week the 1st is and how many days over full weeks there are
' NOTE the Sunday requires a bit extra as Sunday is value 0 and Saturday is value 6
numSat += If((firstDay + (dateDiff Mod 7)) > (DayOfWeek.Saturday), 1, 0)
numSun += If(((firstDay + (dateDiff Mod 7)) > (DayOfWeek.Saturday + 1)) Or (firstDay = DayOfWeek.Sunday And (dateDiff Mod 7 = 1)), 1, 0)
'Output the results
Console.WriteLine("Sats: " & numSat)
Console.WriteLine("Suns: " & numSun)
Console.ReadLine()
1
试试这个:
- 采取月份和年份。
- 在DateTime中获得该月份/月份的第一个月份
- 找到该月份的“结束”,或者更确切地说,是下个月的开始。
- 循环并计算您的DayOfWeek的数量。
Dim month As Integer = 8
Dim year As Integer = 2010
Dim current As New DateTime(year, month, 1)
Dim ending As DateTime = start.AddMonths(1)
Dim numSat As Integer = 0
Dim numSun As Integer = 0
While current < ending
If current.DayOfWeek = DayOfWeek.Saturday Then
numSat += 1
End If
If current.DayOfWeek = DayOfWeek.Sunday Then
numSun += 1
End If
current = current.AddDays(1)
End While
Console.WriteLine("Sats: " & numSat)
Console.WriteLine("Suns: " & numSun)
Console.ReadLine()
相关问题
- 1. 使用月份日历获取选定星期的星期六日期值
- 2. 计算当前年份和当前月份的星期日数量?
- 3. 以星期,星期数,月和年获取日期
- 4. 计算月份和年份的日期
- 5. 月份和年份的日期转换
- 6. 如何统计任意月份的星期数和星期几
- 7. 查找给定月份和特定年份的具体日期
- 8. 给定年份和月份的特定日期的查找日期+ Sql Server 2005
- 9. Scala - 获取特定年份的所有月份和日期
- 10. 如何获得月的星期日和星期六odoo
- 11. 按星期,月份,日期分组
- 12. 给定特定日期或时间戳的特定月份的提取日期和星期数
- 13. 从给定日期获取月份,日期和年份
- 14. 从星期一,星期和年份开始的PHP
- 15. 计算星期六和星期日的数量
- 16. 以Java或JavaScript显示星期五星期五的月份或年份?
- 17. 忽略星期六和星期日
- 18. 计算特定年份内13号的星期五的数量
- 19. 返回月份日期输入星期日期时的数据
- 20. LINQ如何按日期分组日期和月份和年份
- 21. 特定日期和月份的预订
- 22. PHP星期一星期一和星期六日期问题
- 23. 星期六和星期日的行数总和
- 24. 水晶报表中的按年份,月份,星期,日期动态
- 25. 时间到月份星期和星期转换
- 26. 按星期和年份排序Mysql
- 27. NSArray与星期几和一年中的月份
- 28. 获取月份的第一个星期一和最后一个星期日,穿过月份边界
- 29. 开关星期和月份显示日期PHP
- 30. mysql:按年份和月份(无日期)如何做日期
CPU周期与可读性+复杂性+维护! – 2010-11-16 14:48:07
我同意,保持简单(愚蠢)通常是最好的方法。我认为@ p.campbell覆盖了迭代方法,所以发布了替代方案,如果函数在时间关键的应用程序中会非常有用 – DJIDave 2010-11-19 08:03:54