C# 设置一个空的 DateTime 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9982839/
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
Set an empty DateTime variable
提问by phadaphunk
I would declare an empty String variable like this:
我会像这样声明一个空的 String 变量:
string myString = string.Empty;
Is there an equivalent for a 'DateTime' variable ?
“DateTime”变量是否有等价物?
Update :
更新 :
The problem is I use this 'DateTime' as a parameter for a 'StoredProcedure' in SQL. E.g:
问题是我使用这个“DateTime”作为 SQL 中“StoredProcedure”的参数。例如:
DateTime? someDate = null;
myCommand.Parameters.AddWithValue("@SurgeryDate", someDate);
When I run this code an exception is catched telling me the 'StoredProcedure' expected a '@SurgeryDate' parameter. But i provided it. Any idea why?
当我运行此代码时,捕获到一个异常,告诉我 'StoredProcedure' 需要一个 '@SurgeryDate' 参数。但我提供了它。知道为什么吗?
采纳答案by BrokenGlass
Since DateTimeis a value type you cannot assign nullto it, but exactly for these cases (absence of a value) Nullable<T>was introduced - use a nullable DateTimeinstead:
因为DateTime是一个值类型,你不能分配null给它,但正是在这些情况下(没有值)Nullable<T>被引入 - 改用可空值DateTime:
DateTime? myTime = null;
回答by Servy
You may want to use a nullable datetime. Datetime? someDate = null;
您可能想要使用可为空的日期时间。 Datetime? someDate = null;
You may find instances of people using DateTime.Maxor DateTime.Minin such instances, but I highly doubt you want to do that. It leads to bugs with edge cases, code that's harder to read, etc.
您可能会发现有人使用DateTime.Max或DateTime.Min在此类情况下的实例,但我非常怀疑您是否愿意这样做。它会导致边缘情况下的错误、更难阅读的代码等。
回答by Jamie Keeling
There's no such thing as an empty date per se, do you mean something like:
本身没有空日期这样的东西,你的意思是这样的:
DateTime? myDateTime = null;
回答by Shahar Prish
No. You have 2 options:
不,您有 2 个选择:
DateTime date = DateTime.MinValue;
This works when you need to do something every X amount of time (since you will always be over MinValue) but can actually cause subtle errors (such as using some operators w/o first checking if you are not MinValue) if you are not careful.
当您需要每隔 X 时间做某事时(因为您总是会结束MinValue),这会起作用,但MinValue如果您不小心,实际上可能会导致细微的错误(例如使用一些运算符而无需首先检查您是否不这样做)。
And you can use Nullable:
你可以使用Nullable:
DateTime? date = null;
Which is nice and avoids most issues while introducing only 1 or 2.
这很好,避免了大多数问题,而只引入了 1 或 2。
It really depends on what you are trying to achieve.
这真的取决于你想要达到的目标。
回答by Brian Warfield
You can set a DateTime variable to be '1/1/0001 00:00:00' but the variable itself cannot be null. To get this MinTime use:
您可以将 DateTime 变量设置为“1/1/0001 00:00:00”,但变量本身不能为空。要获得此 MinTime,请使用:
DateTime variableName = DateTime.MinValue;
回答by Matt Burland
Option 1: Use a nullable DateTime?
选项 1:使用可为空的 DateTime?
Option 2: Use DateTime.MinValue
选项 2:使用 DateTime.MinValue
Personally, I'd prefer option 1.
就我个人而言,我更喜欢选项 1。
回答by svick
A stringis a sequenceof characters. So it makes sense to have an empty string, which is just an empty sequence of characters.
Astring是一个字符序列。所以有一个 empty 是有意义的string,它只是一个空的字符序列。
But DateTimeis just a single value, so it's doesn't make sense to talk about an “empty” DateTime.
但DateTime只是一个单一的值,所以谈论“空”是没有意义的DateTime。
If you want to represent the concept of “no value”, that's represented as nullin .Net. And if you want to use that with value types, you need to explicitly make them nullable. That means either using Nullable<DateTime>, or the equivalent DateTime?.
如果您想表示“无价值”的概念,则表示为null.Net。如果你想将它与值类型一起使用,你需要明确地使它们可以为空。这意味着要么使用Nullable<DateTime>,要么等效DateTime?。
DateTime(just like all value types) also has a default value, that's assigned to uninitialized fields and you can also get it by new DateTime()or default(DateTime). But you probably don't want to use it, since it represents valid date: 1.1.0001 0:00:00.
DateTime(就像所有值类型一样)也有一个默认值,它被分配给未初始化的字段,你也可以通过new DateTime()或获取它default(DateTime)。但您可能不想使用它,因为它代表有效日期:1.1.0001 0:00:00。
回答by Alex
The method you used (AddWithValue) doesn't convert nullvalues to database nulls. You should use DBNull.Valueinstead:
您使用的方法 ( AddWithValue) 不会将null值转换为数据库空值。你应该使用DBNull.Value:
myCommand.Parameters.AddWithValue(
"@SurgeryDate",
someDate == null ? DBNull.Value : (object)someDate
);
This will pass the someDatevalue if it is not null, or DBNull.Valueotherwise. In this case correct value will be passed to the database.
someDate如果不是null,这将传递该值,DBNull.Value否则将传递该值。在这种情况下,正确的值将被传递到数据库。
回答by iliketocode
Either:
任何一个:
DateTime dt = new DateTime();
or
或者
DateTime dt = default(DateTime);
回答by Chris Catignani
If you set the date to
如果您将日期设置为
DateTime dNewDate = new DateTime();
The value is set to {1/1/0001 12:00:00 AM}
该值设置为 {1/1/0001 12:00:00 AM}

