在将值分配给Nullable DateTime时,字符串未被识别为有效的datetime [英] string was not recognized as a valid datetime while assigning the value to Nullable DateTime

查看:251
本文介绍了在将值分配给Nullable DateTime时,字符串未被识别为有效的datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我以MM / dd / yyyy格式发送日期作为字符串,当我将该值分配给可空的datetime属性值时,获取错误,因为字符串未被识别为有效的datetime,我正在转换字符串如下,然后也得到相同的错误

  private Tbl_UserDetails GetAnnouncementInformation(Tbl_UserDetails userDetails,Dictionary< string,object>细节)
{
userDetails.JoiningDate = string.IsNullOrEmpty(details [JoiningDate] .ToString())?
(DateTime?)null:
DateTime.ParseExact(details [JoiningDate] .ToString(),
MM / dd / yyyy,null);

userDetails.JoiningDate = string.IsNullOrEmpty(details [JoiningDate] .ToString())?
(DateTime?)null:
DateTime.ParseExact(details [JoiningDate] .ToString(),
MM / dd / yyyy,CultureInfo.InvariantCulture);
}

在这两种方式我得到相同的错误。请帮助我。

解决方案

您可以:

  DateTime tempDate; 
userDetails.JoiningDate = DateTime.TryParseExact(
details [JoiningDate] .ToString(),
MM / dd / yyyy,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out tempDate)
? tempDate
:(DateTime?)null;

扩展:

  public static class MyExtensions 
{
public static DateTime? GetNullableDateTime(
this String str,string format =MM / dd / yyyy)
{
DateTime tempDate;
var result = DateTime.TryParseExact(str,format,
CultureInfo.InvariantCulture,DateTimeStyles.None,out tempDate)
? tempDate
:default(DateTime?);
返回结果;
}
}

看起来像:

  userDetails.JoiningDate = 
details [JoiningDate] .ToString()。GetNullableDateTime();

示例程序

 code> Assert.IsNull(sddfsdf.GetNullableDateTime()); 
Assert.IsNotNull(10/20/2014.GetNullableDateTime());
Assert.IsNotNull(20.10.2014.GetNullableDateTime(dd.MM.yyyy));


I have a textbox from which I am sending date as string in 'MM/dd/yyyy' formate, and when I am assigning that value to nullable datetime property value getting the error as string was not recognized as a valid datetime, I am converting the string as below then also getting the same error

private Tbl_UserDetails GetAnnouncementInformation(Tbl_UserDetails userDetails, Dictionary<string, object> details)
{
  userDetails.JoiningDate = string.IsNullOrEmpty(details["JoiningDate "].ToString()) ?
                            (DateTime?)null : 
                             DateTime.ParseExact(details["JoiningDate "].ToString(),
                             "MM/dd/yyyy", null);

  userDetails.JoiningDate = string.IsNullOrEmpty(details["JoiningDate "].ToString()) ?
                            (DateTime?)null : 
                             DateTime.ParseExact(details["JoiningDate "].ToString(),
                             "MM/dd/yyyy", CultureInfo.InvariantCulture);
}

In both the way I am getting the same error. Please help me in this.

解决方案

You could do:

DateTime tempDate;
userDetails.JoiningDate = DateTime.TryParseExact(
  details["JoiningDate "].ToString(), 
  "MM/dd/yyyy", 
  CultureInfo.InvariantCulture, 
  DateTimeStyles.None, 
  out tempDate) 
? tempDate 
: (DateTime?)null;

With extention of :

public static class MyExtensions
{
    public static DateTime? GetNullableDateTime(
        this String str, string format = "MM/dd/yyyy")
    {
        DateTime tempDate;
        var result = DateTime.TryParseExact(str, format, 
            CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate) 
        ? tempDate 
        : default(DateTime?);
        return result;
    }
}   

It would look like:

userDetails.JoiningDate = 
    details["JoiningDate "].ToString().GetNullableDateTime();

Example program

Assert.IsNull("sddfsdf".GetNullableDateTime());
Assert.IsNotNull("10/20/2014".GetNullableDateTime());
Assert.IsNotNull("20.10.2014".GetNullableDateTime("dd.MM.yyyy"));

这篇关于在将值分配给Nullable DateTime时,字符串未被识别为有效的datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆