Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
4.00/5 (4 votes)
Hi
How to assign null value to date? By default my datetime is 1/1/0001, but I want to assign datetime to null. How can I assign null value to datetime?

I tried by this code but it is not working.
C#
dt = Convert.ToDateTime(strposteddate);
strposteddate = dt.ToShortDateString();
if (strposteddate == "1/1/0001")
{
   strposteddate = null;
}
Posted
Updated 5-Nov-18 0:13am
v2
Comments
Suman Zalodiya 21-Jun-13 6:49am    
Please can you tell me the DataType of dt and strposteddate variable to we can clarify it.

You define the datetime variable as nullable:
C#
DateTime? dt = null;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Sep-11 1:19am    
Correct, my 5. OP needs to learn basics of .NET types.
--SA
Wendelius 6-Sep-11 1:27am    
Thanks, I agree
Member 7932936 6-Sep-11 1:20am    
i already define datetime? dt=null;
but it takes default value i.e 1/1/0001
Wendelius 6-Sep-11 1:29am    
If you define the datetime as nullable, it's default value is null, not any date. Especially when you have explicitely set the variable to null, the default doesn't even matter.

Sounds like you're either using a different variable or you set it somewhere else. Use the debugger and with a watch, see the changes that are taking place concerning this variable.
DateTime is not nullable type. You can use Nullable<datetime> dt;
 
Share this answer
 
v2
Comments
Member 7932936 6-Sep-11 1:21am    
I already used like that

but it has set default datetime 1/1/0001
Sergey Alexandrovich Kryukov 6-Sep-11 1:23am    
Don't mix up default time with null. Null should be used to carry semantic if "undetermined time, no time".
--SA
Sergey Alexandrovich Kryukov 6-Sep-11 1:22am    
Correct, my 5. I would prefer the simpler-to-use variant shown by Mika though, please see.
--SA
Well, Here is the fundamental stuff of nullable DateTime. DateTime struct itself does not provide a null option. but we can make it a nullable type which allows you to assign the null to a datetime.

C#
DateTime ? nullableDateTime = null ;
           if(nullableDateTime.HasValue)
           Console.WriteLine(nullableDateTime.Value.ToShortDateString());


As, my nullableDateTime is set to Null, above code block wont print anything.
This will only print a value, if the nullableDateTime has some value.

Come to the minimum date,

var defaultDateTimeValue = default(DateTime);

Here defaultDateTimeValu will be the 1/1/0001 12:00:00 AM}
But, for nullableType,

var defaultDateTimeValue = default(DateTime?);

the value of defaultDateTimeValue should be null.

I hope this will give you enough idea.
 
Share this answer
 
we can't assign null value to datetime

because null value takes the system default date that is minimun date='1/1/0001'
 
Share this answer
 
Null Value pass in datetime field
C#
cmd1.Parameters.AddWithValue("@ups_delivery_date", SqlDbType.DateTime).Value = System.DBNull.Value;
 
Share this answer
 
v2
C#
dt = Convert.ToDateTime(strposteddate);
            strposteddate = dt.ToShortDateString();
            if (strposteddate ==null)
            {
              //allow something
            } 
            else
               
              // allow something
 
Share this answer
 
v2
Comments
Member 7932936 6-Sep-11 1:26am    
if null

my datetime set to 1/1/0001 but hasn't takes null value
Anil Honey 206 6-Sep-11 1:36am    
i have not read the question Properly
We can't assign null value to datetime.
I've made some changes to your code so that it can correctly work.

try
{
dt = Convert.ToDateTime(strposteddate);
}
catch(FormatException)
{
Console.WriteLine("{0} is not in the proper format", strpsoteddate);
strposteddate = "";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900