C# 将日期时间周计算为行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 17:27:46  来源:igfitidea点击:

Calculate DateTime Weeks into Rows

提问by GateKiller

I am currently writing a small calendar in ASP.Net C#. Currently to produce the rows of the weeks I do the following for loop:

我目前正在用 ASP.Net C# 编写一个小日历。目前为了生成周的行,我执行以下 for 循环:

var iWeeks = 6;
for (int w = 0; w < iWeeks; w++) {

This works fine, however, some month will only have 5 weeks and in some rare cases, 4.

这很好用,但是,有些月份只有 5 周,在极少数情况下,只有 4 周。

How can I calculate the number of rows that will be required for a particular month?

如何计算特定月份所需的行数?

This is an example of what I am creating:

这是我正在创建的示例:

enter image description here

在此处输入图片说明

As you can see for the above month, there are only 5 rows required, however. Take the this month (August 2008) which started on a Saturday and ends on a Monday on the 6th Week/Row.

但是,正如您在上个月所看到的,只需要 5 行。以本月(2008 年 8 月)为例,它从星期六开始,到第 6 周/行的星期一结束。

Image found on google

在谷歌上找到的图片



This is an example of what I am creating:

这是我正在创建的示例:

enter image description here

在此处输入图片说明

As you can see for the above month, there are only 5 rows required, however. Take the this month (August 2008) which started on a Saturday and ends on a Monday on the 6th Week/Row.

但是,正如您在上个月所看到的,只需要 5 行。以本月(2008 年 8 月)为例,它从星期六开始,到第 6 周/行的星期一结束。

Image found on google

在谷歌上找到的图片

采纳答案by Serhat Ozgel

Here is the method that does it:

这是执行此操作的方法:

public int GetWeekRows(int year, int month)
{
    DateTime firstDayOfMonth = new DateTime(year, month, 1);
    DateTime lastDayOfMonth = new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
    System.Globalization.Calendar calendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;
    int lastWeek = calendar.GetWeekOfYear(lastDayOfMonth, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    int firstWeek = calendar.GetWeekOfYear(firstDayOfMonth, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    return lastWeek - firstWeek + 1;
}

You can customize the calendar week rule by modifying the System.Globalization.CalendarWeekRule.FirstFourDayWeek part. I hope the code is self explanatory.

您可以通过修改 System.Globalization.CalendarWeekRule.FirstFourDayWeek 部分来自定义日历周规则。我希望代码是不言自明的。

回答by Lasse V. Karlsen

How about checking which week the first and last days will be in?

如何检查第一天和最后一天将在哪一周?

回答by kokos

The months in the Julian / Gregorian calendar have the same number of days each year, except February who can have 28 or 29 days depending on the leapness of the year. You can find the number of days in the Description section at http://en.wikipedia.org/wiki/Gregorian_calendar.

儒略历/格里高利历中的月份每年都有相同的天数,但 2 月可能有 28 或 29 天,具体取决于一年的闰年。您可以在http://en.wikipedia.org/wiki/Gregorian_calendar的描述部分找到天数。

As @darkdog mentioned you have DateTime.DaysInMonth. Just do this:

正如@darkdog 提到的,你有 DateTime.DaysInMonth。只需这样做:

var days = DateTime.DaysInMonth(year, month) + 
           WhatDayOfWeekTheMonthStarts(year, month);    
int rows = (days / 7);  
if (0 < days % 7)  
{  
    ++rows;
}  

Take into consideration the fact that for globalization / localization purposes, some parts of the world use different calendars / methods of organization of the year.

考虑到出于全球化/本地化目的,世界某些地区使用不同的日历/年度组织方法这一事实。

回答by darkdog

Try this,

尝试这个,

DateTime.DaysInMonth

回答by Lasse V. Karlsen

Well, it depends on the culture you're using, but let's assume you can use Thread.CurrentThread.CurrentCulture, then the code to get the week of today would be:

好吧,这取决于您使用的文化,但假设您可以使用 Thread.CurrentThread.CurrentCulture,那么获取今天一周的代码将是:

Culture culture = Thread.CurrentThread.CurrentCulture;
Calendar cal = culture.Calendar;
Int32 week = cal.GetWeekOfYear(DateTime.Today,
    culture.DateTimeFormat.CalendarWeekRule,
    culture.DateTimeFormat.FirstDayOfWeek);

回答by Biri

Check Calendar.GetWeekOfYear. It should do the trick.

检查Calendar.GetWeekOfYear。它应该可以解决问题。

There is a problem with it, it does not follow the 4 day rule by ISO 8601, but otherwise it is neat.

它有一个问题,它不遵循 ISO 8601 的 4 天规则,但除此之外它很整洁。

回答by darkdog

you can get the days of a month by using DateTime.DaysInMonth(int WhichYear,int WhichMonth);

您可以使用 DateTime.DaysInMonth(int whichYear,int whichMonth) 获取一个月的天数;

回答by Lasse V. Karlsen

The problem isn't the number of days in the month, it's how many weeks it spans over.

问题不在于一个月的天数,而在于它跨越了多少周。

February in a non-leap year will have 28 days, and if the first day of the month is a monday, february will span exactly 4 week numbers.

非闰年的二月有 28 天,如果该月的第一天是星期一,二月将正好跨越 4 个星期。

However, if the first day of the month is a tuesday, or any other day of the week, february will span 5 week numbers.

但是,如果该月的第一天是星期二或一周中的任何其他日期,则 2 月将跨越 5 个星期。

A 31 day month can span 5 or 6 weeks the same way. If the month starts on a monday, the 31 days gives you 5 week numbers. If the month starts on saturday or sunday, it will span 6 week numbers.

一个 31 天的月可以以同样的方式跨越 5 或 6 周。如果月份从星期一开始,则 31 天为您提供 5 个周数。如果该月从星期六或星期日开始,它将跨越 6 个星期。

So the right way to obtain this number is to find the week number of the first and last days of the month.

所以获得这个数字的正确方法是找到一个月的第一天和最后一天的周数。



Edit #1: Here's how to calculate the number of weeks a given month spans: Edit #2: Fixed bugs in code

编辑#1:以下是计算给定月份跨度的周数的方法: 编辑#2:修复代码中的错误

public static Int32 GetWeekForDateCurrentCulture(DateTime dt)
{
    CultureInfo culture = Thread.CurrentThread.CurrentCulture;
    Calendar cal = culture.Calendar;
    return cal.GetWeekOfYear(dt,
        culture.DateTimeFormat.CalendarWeekRule,
        culture.DateTimeFormat.FirstDayOfWeek);
}

public static Int32 GetWeekSpanCountForMonth(DateTime dt)
{
    DateTime firstDayInMonth = new DateTime(dt.Year, dt.Month, 1);
    DateTime lastDayInMonth = firstDayInMonth.AddMonths(1).AddDays(-1);
    return
        GetWeekForDateCurrentCulture(lastDayInMonth)
        - GetWeekForDateCurrentCulture(firstDayInMonth)
        + 1;
}

回答by DevelopingChris

First Find out which weekday the first day of the month is in. Just new up a datetime with the first day, always 1, and the year and month in question, there is a day of week property on it.

首先找出该月的第一天在哪个工作日。只需将日期时间与第一天(始终为 1)以及所讨论的年份和月份进行更新,其中有一个星期几属性。

Then from here, you can use the number of days in the month, DateTime.DaysInMonth, in order to determine how many weeks when you divide by seven and then add the number of days from 1 that your first day falls on. For instance,

然后从这里开始,您可以使用一个月中的天数,DateTime.DaysInMonth以确定您除以 7 的周数,然后从 1 开始添加您的第一天所在的天数。例如,

public static int RowsForMonth(int month, int year)
{
    DateTime first = new DateTime(year, month, 1);

    //number of days pushed beyond monday this one sits
    int offset = ((int)first.DayOfWeek) - 1;

    int actualdays = DateTime.DaysInMonth(month, year) +  offset;

    decimal rows = (actualdays / 7);
    if ((rows - ((int)rows)) > .1)
    {
        rows++;
    }
    return rows;
}