【问题标题】:Get the start and end date of a given week year, given month & given week获取给定周年、给定月份和给定周的开始和结束日期
【发布时间】:2011-07-29 10:54:24
【问题描述】:

如何在 c# 4.0 中获取给定年份 (int)、给定月份 (int) 和给定周 (int) { example Year : 2011 Month: 07 week: 04 } 的开始和结束日期?提前致谢。

2011 年的开始日期为 07 月,该月的周数为 04。

【问题讨论】:

  • “一周”开始时您的规则是什么?那里有很多的可能性。
  • 等等等等。您的问题是“在给定年、月和周数的情况下,我如何获得一周的开始和结束日期”??我不明白给定年份的开始和结束日期是什么意思。不就是 1 月 1 日和 12 月 31 日吗?如果这是您的问题,请参阅:stackoverflow.com/questions/662379/…

标签: c# .net c#-4.0


【解决方案1】:

Google 是你的朋友。

月:

public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
{
   return new DateTime(dateTime.Year, dateTime.Month, 1);
}


public DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
{
   DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
   return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}

你可以多年做类似的事情:

   DateTime time = new DateTime(2011,1,1);
   time.AddYears(1).AddDays(-1);

week 需要使用 CultureInfo.FirstDay(或者您想设置为一周的第一天的任何内容,在某些国家/地区是星期一,有时是星期日)。

/// <summary>
    /// Returns the first day of the week that the specified
    /// date is in using the current culture. 
    /// </summary>
    public static DateTime GetFirstDayOfWeek(DateTime dayInWeek)
    {
        CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
        return GetFirstDateOfWeek(dayInWeek, defaultCultureInfo);
    }

    /// <summary>
    /// Returns the first day of the week that the specified date 
    /// is in. 
    /// </summary>
    public static DateTime GetFirstDayOfWeek(DateTime dayInWeek, CultureInfo cultureInfo)
    {
        DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
        DateTime firstDayInWeek = dayInWeek.Date;
        while (firstDayInWeek.DayOfWeek != firstDay)
            firstDayInWeek = firstDayInWeek.AddDays(-1);

        return firstDayInWeek;
    }

【讨论】:

  • 有用的代码片段,但我认为它根本与原始问题无关(除非你可以使用 FirstDayOfMonthFromDateTime(),我想)。
【解决方案2】:

不确定,但这是你所追求的吗?

var weekStart = new DateTime(year, month, 1).AddDays(week * 7);
var weekEnd = weekStart.AddDays(6);

【讨论】:

    【解决方案3】:

    假设您从第 1 周开始:

    var startDate = new DateTime(year, month, 1).AddDays((week - 1) * 7);
    var endDate = startDate.AddDays(6);
    

    【讨论】:

      【解决方案4】:

      你也可以使用

      DateTime.DaysInMonth(int year,int month);
      

      想办法。这几周会更加困难。

      【讨论】:

        【解决方案5】:

        DateTime 计算,因为这些有点棘手,我可以提出一些假设

        //assign it to the first day of the month
        DateTime getweek = new DateTime(2011, 4, 1);
        // say the week starts on a Sunday
        while (getweek.DayOfWeek != DayOfWeek.Sunday)
              getweek = getweek.AddDays(1);
        DateTimeFormatInfo info = DateTimeFormatInfo.CurrentInfo;            
        Calendar cal = info.Calendar;
        //Now you are on the first week add 3 more to move to the Fourth week
        DateTime start = cal.AddWeeks(getweek, 3); // 24 April 2011
        DateTime end = start.AddDays(6); // 30 April 2011
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-08
          • 1970-01-01
          • 2013-10-13
          • 1970-01-01
          相关资源
          最近更新 更多