【问题标题】:DateTime getting todays Month in if statementDateTime 在 if 语句中获取今天的月份
【发布时间】:2011-08-15 17:30:15
【问题描述】:

我想做一个 if 语句 if month == january 例如加载一些东西 else if month == april 加载其他内容。 有人可以帮忙吗 谢谢

【问题讨论】:

    标签: c#


    【解决方案1】:

    为 Month 做一个枚举并使用这个 case 语句

    switch (DateTime.Now.Month)
    {
         case MonthEnum.JAN
            break;
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用 DateTime.Now.Month

              if(DateTime.Now.Month == 1)
              {
                  //January
              }
              else if (DateTime.Now.Month == 2)
              {
                  //February
              }
              //etc
      

      【讨论】:

        【解决方案3】:

        您可以像这样使用一年中的月份创建一个枚举:

        public enum Months{
             January = 1,
             February = 2,
             .
             .
             December = 12
        }
        

        然后试试

        if(Datetime.Now.Month == (int)Months.January){
        
          //Do Something...
        
        } else if(Datetime.Now.Month == (int)Months.April){
        
          //Do Something else
        
        }
        

        希望这会有所帮助。问候,

        胡安·阿尔瓦雷斯

        【讨论】:

          【解决方案4】:
          string monthName = DateTime.Now.ToString("MMMM");
          if(monthName == "april"{
          ...
          }
          

          【讨论】:

            【解决方案5】:
            switch (DateTime.Now.Month)
            {
                 case 1: // JAN
                    ...
                    break;
                 case 2: // FEB
                    ...
                    break;
            }
            

            【讨论】:

              【解决方案6】:

              使用DateTime.Month 属性检查月份

              switch (DateTime.Now.Month){
                case 1://January stuff here
                break;
                case 2://Feb stuff here etc...
              }
              

              【讨论】:

                【解决方案7】:
                【解决方案8】:

                Is there a predefined enumeration for Month in the .NET library?

                string monthName = CultureInfo.CurrentCulture.DateTimeFormat
                    .GetMonth ( DateTime.Now.Month );
                
                switch (monthName)
                {
                    case "January":
                        // do something
                        break;
                    case "April":
                        // do something
                        break;
                    // etc.
                }
                

                【讨论】:

                  【解决方案9】:

                  几种方法

                  if(DateTime.Today.Month == 1){
                    // do something
                  }
                  
                  if(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month) == "January"){
                    // do something
                  }
                  

                  对于第二种方法,您需要包含 System.Globalization

                  【讨论】:

                    【解决方案10】:

                    你可以使用Month属性,范围是1-12:

                    int month = DateTime.Now.Month;
                    if(month == 4) //April
                    {..
                    

                    【讨论】:

                      【解决方案11】:
                      var now = DateTime.Now;
                      var monthName = now.ToString("MMMM")
                      if (monthName == "January)
                      {
                        //load something 
                      }
                      else if (monthName == "April")
                      {
                        //load something else. 
                      }
                      

                      【讨论】:

                      • 在非英语文化中设置的计算机是否会出现问题?他们不会返回不同的字符串吗?
                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2012-08-17
                      • 1970-01-01
                      • 1970-01-01
                      • 2010-10-13
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多