获取给定周年、给定月份和日期的开始和结束日期给定的一周

发布于 2024-11-27 05:26:35 字数 149 浏览 2 评论 0原文

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

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

How can I get the start and end date of a given year (int), given month (int), and given week (int) { example Year : 2011 Month: 07 week: 04 } in c# 4.0? Thanks in advance.

The Start Date of the Year 2011 Month 07 and the week number of the month is 04.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

﹉夏雨初晴づ 2024-12-04 05:26:36

你也可以用

DateTime.DaysInMonth(int year,int month);

它来弄清楚。这几周将会更加困难。

You could also use

DateTime.DaysInMonth(int year,int month);

to figure it out. The weeks will be more difficult.

带上头具痛哭 2024-12-04 05:26:36

日期时间计算,因为这些有点棘手,通过一些假设我可以想出这个

//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

DateTime calculations as these are a bit tricky, with some assumptions i could come up with this

//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
甩你一脸翔 2024-12-04 05:26:35

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;
    }

Google is your friend.

Months:

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);
}

You can do something similar for years:

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

And week needs to use the CultureInfo.FirstDay (or whatever you want to set as the first day of a week, in some countries it's Monday, sometimes it's Sunday).

/// <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;
    }
初相遇 2024-12-04 05:26:35

不确定,但这就是你想要的吗?

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

Not sure, but is this what you're after?

var weekStart = new DateTime(year, month, 1).AddDays(week * 7);
var weekEnd = weekStart.AddDays(6);
岁月蹉跎了容颜 2024-12-04 05:26:35

假设您从第一周开始:

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

Assuming you start with week 1:

var startDate = new DateTime(year, month, 1).AddDays((week - 1) * 7);
var endDate = startDate.AddDays(6);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文