How to get the name of each day in next month?

后端 未结 5 489
无人共我
无人共我 2021-01-23 01:35
DateTime dt = new DateTime();
dt = DateTime.Now.AddMonths(1);
int x = DateTime.DaysInMonth(dt.Year, dt.Month);
MessageBox.Show(x.ToString());  // works ok - 31


        
相关标签:
5条回答
  • 2021-01-23 01:41

    You can also leverage Enumerable.Range and LINQ (and the DaysOfWeek enum)

    DateTime dt = DateTime.Now.AddMonths(1);
    
    Enumerable.Range(1, DateTime.DaysInMonth(dt.Year, dt.Month))
        .Select(dayNumber => new DateTime(dt.Year, dt.Month, dayNumber))
        .Select(dayName => dayName.DayOfWeek.ToString()).ToList()
        .ForEach(day => MessageBox.Show(day));
    

    The two .Select()s can be merged but I kept them separated for readability.

    0 讨论(0)
  • 2021-01-23 01:42

    Try DateTime.ToString(format) method.

    DateTime dt = new DateTime();
    String dayName=dt.ToString("ffffd");
    
    0 讨论(0)
  • 2021-01-23 01:56

    try this

    string day = DateTime.Now.ToString("ffffd");
    
    0 讨论(0)
  • 2021-01-23 01:57

    This should do the job.

    DateTime Base = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1);
    int x = DateTime.DaysInMonth(Base.Year, Base.Month);
    while (Base.Day != x)
    {
        Console.WriteLine(Base.DayOfWeek.ToString());
        Base = Base.AddDays(1);
    }
    
    0 讨论(0)
  • 2021-01-23 02:05

    You can do:

    DateTime dt = new DateTime();
    dt = DateTime.Now.AddMonths(1);
    DateTime conditionDateTime = new DateTime(dt.Year, dt.Month,DateTime.DaysInMonth(dt.Year, dt.Month));
    
     for (DateTime dt1 = new DateTime(dt.Year, dt.Month, 1); dt1 <conditionDateTime; dt1 = dt1.AddDays(1))
         {
            Console.Write(dt1.ToShortDateString() + " : ");
            Console.WriteLine(dt1.DayOfWeek);
         }
    

    This will give you

    8/1/2012 : Wednesday
    8/2/2012 : Thursday
    8/3/2012 : Friday
    8/4/2012 : Saturday
    8/5/2012 : Sunday
    8/6/2012 : Monday
    8/7/2012 : Tuesday
    8/8/2012 : Wednesday
    8/9/2012 : Thursday
    8/10/2012 : Friday
    8/11/2012 : Saturday
    8/12/2012 : Sunday
    8/13/2012 : Monday
    8/14/2012 : Tuesday
    8/15/2012 : Wednesday
    8/16/2012 : Thursday
    8/17/2012 : Friday
    ......
    
    0 讨论(0)
提交回复
热议问题