C# 如何转换日期时间?到日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14144473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:51:03  来源:igfitidea点击:

how to convert datetime? to datetime

c#asp.net

提问by

I have filed txtb_dateOfServiceis required to complete the form but if txtb_dateOfServiceis emapty return null if not TryParse the date >I had this error I don't know how to fix it

我已经提交txtb_dateOfService了完成表格但如果txtb_dateOfService是 emapty 返回 null 如果不是 TryParse>我遇到这个错误的日期我不知道如何解决它

The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments

'System.DateTime.TryParse(string, out System.DateTime)' 的最佳重载方法匹配有一些无效参数

    DateTime? dateOfService= null;
    if (string.IsNullOrEmpty(txtb_dateOfService.Text))
    {
        dateOfService = null;

    }
    else
        if (DateTime.TryParse(txtb_dateOfService.Text, out dateOfService))
        {

        }

采纳答案by dasblinkenlight

You cannot pass a reference to DateTime?into a method expecting DateTime. You can solve this by introducing a temporary variable, like this:

您不能将引用传递DateTime?到期望 的方法中DateTime。您可以通过引入一个临时变量来解决这个问题,如下所示:

else { // <<=== This is the final "else" from your code
    DateTime tmp;
    if (DateTime.TryParse(txtb_dateOfService.Text, out tmp))
    {
        dateOfService = tmp;
    } else {
        dateOfService = null;
    }
}

回答by Preston Guillot

Your problem is converting DateTime?to DateTime, not vice versa. The DateTime.TryParsemethod's outparameter is not nullable; in the event TryParsefails the out parameter will be assigned DateTime.MinValueas its value. There is no reason to declare your dateOfServicevariable as a nullable type from this snippet.

您的问题正在转换DateTime?DateTime,而不是相反。该DateTime.TryParse方法的out参数不可为空;如果TryParse失败,out 参数将被分配DateTime.MinValue为其值。没有理由在dateOfService此代码段中将您的变量声明为可空类型。

回答by D Stanley

You can either throw an exception if the parse fails:

如果解析失败,您可以抛出异常:

DateTime? dateOfService= null;
if (string.IsNullOrEmpty(txtb_dateOfService.Text))
{
    dateOfService = null;
}
else
{
    // will throw an exception if the text is not parseable
    dateOfService = DateTime.Parse(txtb_dateOfService.Text);  
}

or use an intermediate DateTime to store the parsed result:

或使用中间 DateTime 来存储解析结果:

DateTime? dateOfService= null;
if (string.IsNullOrEmpty(txtb_dateOfService.Text))
{
    dateOfService = null;

}
else
{
    DateTime temp;
    if (DateTime.TryParse(txtb_dateOfService.Text, out temp))
    {
        dateOfService = temp;
    } 
    else 
    {
        dateOfService = null;
    }
}

Either of these can be logically simplified; I'm showing the full breakout to convey the logic.

其中任何一个都可以在逻辑上简化;我正在展示完整的突破来传达逻辑。

回答by Felipe Oriani

You can try to convert your stringin a DateTime

您可以尝试将您stringDateTime

DateTime? dataOfService = null;
DateTime output;

if (DateTime.TryParse(txtb_dateOfService.Text, out output))
   dataOfService = output;

now you can use dataOfServiceas a Nullable<DateTime>and check if it has a valid data converted using HasValueand Valueproperties.

现在您可以dataOfService用作 aNullable<DateTime>并检查它是否具有使用HasValueValue属性转换的有效数据。

回答by Charles Lambert

you need to create a temp value to hold the out parameter of TryParse:

您需要创建一个临时值来保存以下参数TryParse

DateTime tmp;
if (DateTime.TryParse(txtb_dateOfService.Text, out tmp)) {
    dateOfService = tmp;
} else{
    dateOfService = null;
}

A more terse example

一个更简洁的例子

DateTime tmp;
DateTime? dateOfService = DateTime.TryParse(txtb_dateOfService.Text, out tmp)
   ? tmp
   : (DateTime?)null;

回答by It'sNotALie.

try out dateOfService.Value, this should work (I think)

尝试 dateOfService.Value,这应该可行(我认为)