C# 不能隐式转换类型“System.DateTime?” 到'System.DateTime'。存在显式转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19972965/
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
Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists
提问by
I am trying to convert datetime? todatetime but I get this Error:
我想转换日期时间?todatetime 但我收到此错误:
Error 7 Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists
错误 7 无法隐式转换类型“System.DateTime?” 到'System.DateTime'。存在显式转换
Here is my code:
这是我的代码:
public string ConvertToPersianToShow(DateTime? datetime)
{
DateTime dt;
string date;
dt = datetime;
string year = Convert.ToString(persian_date.GetYear(dt));
string month = Convert.ToString(persian_date.GetMonth(dt));
string day = Convert.ToString(persian_date.GetDayOfMonth(dt));
if (month.Length == 1)
{
month = "0" + Convert.ToString(persian_date.GetMonth(dt));
}
if (day.Length == 1)
{
day = "0" + Convert.ToString(persian_date.GetDayOfMonth(dt));
}
//date = Convert.ToString(persian_date.GetYear(dt)) + "/" +
Convert.ToString(persian_date.GetMonth(dt)) + "/" +
//Convert.ToString(persian_date.GetDayOfMonth(dt));
date = year + "/" + month + "/" + day+"("+dt.Hour+":"+dt.Minute+")";
return date;
}
采纳答案by Kamil Budziewski
You have 3 options:
您有 3 个选择:
1) Get default value
1) 获取默认值
dt = datetime??DateTime.Now;
it will assign DateTime.Now
(or any other value which you want) if datetime
is null
DateTime.Now
如果datetime
为空,它将分配(或您想要的任何其他值)
2) Check if datetime contains value and if not return empty string
2) 检查日期时间是否包含值,如果不包含则返回空字符串
if(!datetime.HasValue) return "";
dt = datetime.Value;
3) Change signature of method to
3)将方法的签名更改为
public string ConvertToPersianToShow(DateTime datetime)
It's all because DateTime?
means it's nullable DateTime
so before assigning it to DateTime
you need to check if it contains value and only then assign.
这一切都是因为DateTime?
意味着它可以为空,DateTime
因此在将其分配给DateTime
您之前需要检查它是否包含值,然后才分配。
回答by David Pilkington
dt
is nullable
you need to access its Value
dt
是nullable
你需要访问它的Value
if (datetime.HasValue)
dt = datetime.Value;
It is important to remember that it can be NULL
. That is why the nullable
struct has the HasValue
property that tells you if it is NULL
or not.
重要的是要记住它可以NULL
。这就是为什么nullable
结构具有HasValue
告诉您它是否存在的属性的原因NULL
。
You can also use the null-coalescing operator
??
to assign a default value
您还可以使用来分配默认值null-coalescing operator
??
dt = datetime ?? DateTime.Now;
This will assign the value on the right if the value on the left is NULL
如果左侧的值是 NULL
回答by Ahmed ilyas
you should be using the .Value of the datetime parameter. All Nullable structs have a value property which returns the concrete type of the object. but you must check to see if it is null beforehand otherwise you will get a runtime error.
您应该使用 datetime 参数的 .Value 。所有 Nullable 结构都有一个 value 属性,它返回对象的具体类型。但是您必须事先检查它是否为空,否则您将收到运行时错误。
i.e:
IE:
datetime.Value
but check to see if it has a value first!
但首先检查它是否有值!
if (datetime.HasValue)
{
// work with datetime.Value
}
回答by dr.Crow
The problem is that you are passing a nullable typeto a non-nullable type.
问题是您将可空类型传递给不可空类型。
You can do any of the following solution:
您可以执行以下任一解决方案:
A. Declare your dt
as nullable
A. 声明你dt
为可空
DateTime? dt = dateTime;
DateTime? dt = dateTime;
B. Use Value
property of the the DateTime? datetime
B. 使用Value
属性DateTime? datetime
DateTime dt = datetime.Value;
DateTime dt = datetime.Value;
C. Cast it
C. 投射
DateTime dt = (DateTime) datetime;
DateTime dt = (DateTime) datetime;