循环周基于文化信息

时间:2011-05-13 07:15:18

标签: c# asp.net datetime cultureinfo

如何基于文化信息循环整周(星期一 - 星期日),所以在我的情况下,星期一将是一周的第一天?是否有可能同时找到当天的int值?

对于某些信息:我需要这样做,以便为商店开出一些时间。

6 个答案:

答案 0 :(得分:8)

我认为您需要的是以下循环。

        DayOfWeek firstDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
        for (int dayIndex = 0; dayIndex < 7; dayIndex++)
        {
            var currentDay = (DayOfWeek) (((int) firstDay + dayIndex) % 7);

            // Output the day
            Console.WriteLine(dayIndex + " " + currentDay);
        }

模7非常重要,因为每周的第一天都会因不同的文化而异。

答案 1 :(得分:1)

这将为您提供特定文化中一周的第一天。

DayOfWeek firstDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

这可能随后......

int firstDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

DayOfWeek.Sunday = 0

DayOfWeek.Saturday = 6

你会像任何其他int一样迭代它。

http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx

答案 2 :(得分:0)

DateTime Dt = new DateTime(2011,5,13,0,0,0);

int WeeklyOffValue = (int)Dt.DayOfWeek

答案 3 :(得分:0)

Time Period Library for .NET包含类,并支持文化:

// ----------------------------------------------------------------------
public void WeekDaysSample()
{
  Week week = new Week( new DateTime( 2011, 05, 13 ) );
  foreach ( Day day in week.GetDays() )
  {
    Console.WriteLine( "Day: {0}, DayOfWeek: {1}, Int: {2}", day, day.DayOfWeek, (int)day.DayOfWeek );
    // > Day: Montag; 09.05.2011 | 0.23:59, DayOfWeek: Monday, Int: 1
    // > Day: Dienstag; 10.05.2011 | 0.23:59, DayOfWeek: Tuesday, Int: 2
    // > Day: Mittwoch; 11.05.2011 | 0.23:59, DayOfWeek: Wednesday, Int: 3
    // > Day: Donnerstag; 12.05.2011 | 0.23:59, DayOfWeek: Thursday, Int: 4
    // > Day: Freitag; 13.05.2011 | 0.23:59, DayOfWeek: Friday, Int: 5
    // > Day: Samstag; 14.05.2011 | 0.23:59, DayOfWeek: Saturday, Int: 6
    // > Day: Sonntag; 15.05.2011 | 0.23:59, DayOfWeek: Sunday, Int: 0
  }
} // WeekDaysSample

即使在今天,也就是13日星期五,这仍然有效。)

答案 4 :(得分:0)

for (int i = 1; i <= 7; i++)
   Console.WriteLine(new DateTime(2014, 6, i).ToString("DDDD", culture));

2014年7月1日 - 周日

答案 5 :(得分:-1)

我相信你想要整个星期,就像这样

foreach (DayOfWeek dy in Enum.GetValues(typeof(DayOfWeek)))
{
     dy.ToString() // this would be Sunday, monday ......
}