3

私は(現在の日付について)私に与えるこの関数を持っています-その週番号:

だから:DateTime(2009,1,1)

 CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(new DateTime(2009,1,1), CalendarWeekRule.FirstDay, DayOfWeek.Sunday).Dump();

答え :1

および :DateTime(2009,1,4)

答え :2

ここに画像の説明を入力

今、私はこの値の startDate && endDate を与える関数が必要です:

so for week #1 -> 1/1/2009   ---> 1/3/2009
so for week #2 -> 1/4/2009   ---> 1/10/2009

したがって、指定された日付の週番号を取得する関数があります。しかし、今週はx---> y

x と y が必要です。

ありがとう。

ps - このような機能を探していましたが、見つかりませんでした。:-(

4

3 に答える 3

1
using System;
using System.Globalization;

public static class FirstDayOfWeekUtility
{
    /// <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;
    }
}
于 2012-07-03T11:16:19.470 に答える
1

週の最初の日を日付で取得するには:

static DateTime GetFirstDayOfWeek(DateTime date)
{
    var firstDayOfWeek = date.AddDays(-((date.DayOfWeek - DayOfWeek.Sunday + 7) % 7));
    if (firstDayOfWeek.Year != date.Year)
        firstDayOfWeek = new DateTime(date.Year, 1, 1);
    return firstDayOfWeek;
}

週の最後の日も同じように機能します。

static DateTime GetLastDayOfWeek(DateTime date)
{
    var lastDayOfWeek = date.AddDays((DayOfWeek.Saturday - date.DayOfWeek + 7) % 7);
    if (lastDayOfWeek.Year != date.Year)
        lastDayOfWeek = new DateTime(date.Year, 12, 31);
    return lastDayOfWeek;
}

Royiの追加(最終):

単一の日付からすべての詳細(週の詳細)を提供する拡張メソッド:ps週の最初の日=日曜日。

   public class DateTimeSpan
    {
        public DateTime WeekStartDate;
        public DateTime WeekEndDate;
        public DateTime MonthStartDate;
        public DateTime MonthEndDate;
        public DateTime YearStartDate;
        public DateTime YearEndDate;
        public int WeekNum;

    }

    public static DateTimeSpan TimeProperties(this DateTime str)
    {
        if (str == null) return null;
        DateTimeSpan dts = new DateTimeSpan();
        dts.WeekNum=     CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(str, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        dts.WeekStartDate = GetFirstDayOfWeek(str);
        dts.WeekEndDate = GetLAstDayOfWeek(str);
        dts.MonthStartDate = new DateTime(str.Year, str.Month, 1);
        int numberOfDays = DateTime.DaysInMonth(str.Year, str.Month);
        DateTime last = new DateTime(str.Year, str.Month, numberOfDays);
        dts.MonthEndDate = last;
        dts.YearStartDate = new DateTime(str.Year, 1, 1);
        numberOfDays = DateTime.DaysInMonth(str.Year, 12);
        last = new DateTime(str.Year, 12, numberOfDays);
        dts.YearEndDate = last;

        return dts;
    }


    static DateTime GetFirstDayOfWeek(DateTime date)
    {
        var firstDayOfWeek = date.AddDays(-((date.DayOfWeek - DayOfWeek.Sunday + 7) % 7));
        if (firstDayOfWeek.Year != date.Year)
            firstDayOfWeek = new DateTime(date.Year, 1, 1);
        return firstDayOfWeek;
    }

    static DateTime GetLAstDayOfWeek(DateTime date)
    {
        var firstDayOfWeek = date.AddDays(((DayOfWeek.Saturday - date.DayOfWeek + 7) % 7));
        if (firstDayOfWeek.Year != date.Year)
            firstDayOfWeek = new DateTime(date.Year, 12, 31);
        return firstDayOfWeek;
    }
于 2012-07-03T11:42:09.127 に答える
1

私はかつて、この投稿のコメントに記載されている方法の1つを使用しました http://joelabrahamsson.com/entry/getting-the-first-date-in-a-week-with-c-sharp

于 2012-07-03T11:15:03.940 に答える