如何将 C# 中的日期与“1/1/0001 12:00:00 AM”进行比较)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17549426/
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
How can I compare a date in C# to "1/1/0001 12:00:00 AM")
提问by
I am trying the following:
我正在尝试以下操作:
if (e.CreatedDate == "1/1/0001 12:00:00 AM")
But this gives me an error saying I cannot compare a date to a string. How can I make it so I check the CreatedDate is equal to the "1/1/0001 12:00:00 AM" which is I guess the default?
但这给了我一个错误,说我无法将日期与字符串进行比较。我怎样才能做到这一点,所以我检查 CreatedDate 是否等于“1/1/0001 12:00:00 AM”,这是我猜的默认值?
采纳答案by Andrei
You can use DateTime.MinValue, which has exactly the same value:
您可以使用DateTime.MinValue,它具有完全相同的值:
if (e.CreatedDate == DateTime.MinValue)
回答by lightbricko
回答by Praveen
"1/1/0001 12:00:00 AM" this is a stringdatatype. so convert it to DateTime.
“1/1/0001 12:00:00 AM”这是一种string数据类型。所以将其转换为DateTime.
if (e.CreatedDate == Convert.ToDateTime("1/1/0001 12:00:00 AM"))
{
//--- To Dos
}
But .NET Framework provide a default way to check this using
但是 .NET Framework 提供了一种默认方式来使用
if (e.CreatedDate.Equals(DateTime.MinValue))
if (e.CreatedDate.Equals(DateTime.MinValue))
回答by DoctorMick
use DateTime.MinValue
使用 DateTime.MinValue
if (e.CreatedDate.Equals(DateTime.MinValue))
回答by Kevin Brechbühl
You can check the default value with DateTime.MinValue:
您可以使用以下命令检查默认值DateTime.MinValue:
if (e.CreatedDate == DateTime.MinValue)
回答by LittleSweetSeas
You should use DateTime.Compare(DateTime, DateTime) function, which returns an integer. Eg.
您应该使用 DateTime.Compare(DateTime, DateTime) 函数,它返回一个整数。例如。
if (DateTime.Compare(DateTime.MinValue, e.CreatedDate) == 0){
...
}
回答by Paciente8159
I assume e.CreateDate is of type DateTime.
我假设 e.CreateDate 是 DateTime 类型。
Try it like:
像这样尝试:
if (e.CreatedDate == DateTime.Parse("1/1/0001 12:00:00 AM"))
If your aiming to compare if it's equal to the minimum date then just compare it to DateTime.MinValue. The above example is more generic.
如果您的目标是比较它是否等于最小日期,那么只需将它与 DateTime.MinValue 进行比较。上面的例子更通用。
回答by Sebastien
You need to put "1/1/0001 12:00:00 AM" into a date format to compare it in your if.
您需要将“1/1/0001 12:00:00 AM”放入日期格式中,以便在您的 if 中进行比较。
if(e.CreatedDate == ((Date)"1/1/0001 12:00:00 AM"))
回答by cui peng
maybe you need a string compare ,so you can do it like this
也许你需要一个字符串比较,所以你可以这样做
e.CreatedDate.ToString(FORMAT)=="1/1/0001 12:00:00 AM"

