【问题标题】:How to convert to DateTime but have default value if input is blank?如果输入为空白,如何转换为 DateTime 但具有默认值?
【发布时间】:2013-02-12 07:03:26
【问题描述】:

如果输入为空,什么.NET 函数将转换为DateTime 但具有默认值?

例如。

DateTime dtTest = Convert.ToDateTime(getDateString());

如果getDateString() 返回一个空字符串Convert.ToDateTime 会抛出异常。

我怎样才能使用默认值"9:00AM" 而不是空字符串?这是可以使用TryParse 的东西吗?

【问题讨论】:

  • 好吧,你确实可以使用TryParse,但我不确定9:00AM 是否真的是DateTime,或者你的意思是那个时间的当前日期。

标签: c# .net datetime tryparse


【解决方案1】:

使用DateTime.TryParse,如果解析失败,您可以分配DateTime.MinValue.AddHours(9) 以获取具有最小日期的(9:00AM) 时间。

string str = "";
DateTime temp;
DateTime dt = DateTime.TryParse(str, out temp) ? temp : DateTime.MinValue.AddHours(9);

对于上述代码,您的 dt 对象将持有 {01/01/0001 9:00:00 AM}

【讨论】:

    【解决方案2】:

    我使用了一种叫做三元语句的东西。您可以在此处查看示例:MSDN

        string myDate = getDateString();
        DateTime dtTest = Convert.ToDateTime(String.IsNullOrEmpty(myDate) ? "my default value" : myDate);
    

    三元语句如下:

    String.IsNullOrEmpty(myDate) ? "my default value" : myDate
    

    可以这样解读:

    如果字符串为 null 或空,则使用“我的默认值”,否则使用 myDate。

    【讨论】:

      【解决方案3】:

      你应该把它分成两部分:

      • 找出要解析的字符串
      • 如果该字符串无法被解析
      • ,请找出你想要的结果

      例如:

      string text = GetDateString(); // Name converted to follow .NET conventions
      if (string.IsNullOrEmpty(text))
      {
          text = "9:00AM"; // Or whatever
      }
      
      DateTime parsed;
      if (!DateTime.TryParse(text, out parsed))
      {
          parsed = // some default here
      }
      

      如果您知道您所期望的格式以及您希望将其解析为哪种文化,也可以考虑使用TryParseExact 而不是TryParse

      使用默认 string 值的替代方法是仅使用默认 DateTime 值,如果文本为 null,则无需解析:

      string text = GetDateString(); // Name converted to follow .NET conventions
      DateTime parsed;
      
      // This will only try to parse if text is non-null and non-empty
      if (string.IsNullOrEmpty(text) || !DateTime.TryParse(text, out parsed))
      {
          parsed = // some default here
      }
      

      【讨论】:

        【解决方案4】:

        只需编写一个包装函数,当验证失败时返回默认值如下,

        public DateTime Validate(string dateString)
        {
                DateTime dt;
                if(DateTime.TryParse(dateString, out dt))
                    return dt;
                else
                    return DateTime.Now; //default value
        
        
        }
        

        【讨论】:

        • 你不能设置 DateTime dt=null
        【解决方案5】:
          DateTime ToDateTime(string sourceValue){
            DateTime result;
            if(DateTime.TryParse(sourceValue, out result)
            return result
            else{
            result=new DateTime(2013,1,1) //default value, whatever you want
        return result
            }
            }
        

        【讨论】:

          【解决方案6】:

          就这样吧:

          DateTime MyDate;
          DateTime ParsedDate;
          
          if (DateTime.TryParse(txtNotificationDate.Text.Trim(), out ParsedDate))
          {
              MyDate= ParsedDate;
          }
          else
          {
              MyDate = DateTime.Now;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-25
            • 2021-08-27
            • 2019-09-07
            • 2013-07-27
            • 1970-01-01
            • 2019-03-09
            相关资源
            最近更新 更多