Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
        dt = Convert.ToDateTime(strposteddate);
        strposteddate = dt.ToShortDateString();
        if (strposteddate == "1/1/0001")
        {
           strposteddate = null;
        } 
Posted 5-Sep-11 19:13pm
Edited 5-Sep-11 19:15pm
Prerak Patel139.7K
v2
Comments
suman161288 at 21-Jun-13 6:49am
   
Please can you tell me the DataType of dt and strposteddate variable to we can clarify it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You define the datetime variable as nullable:
DateTime? dt = null;
  Permalink  
Comments
SAKryukov at 6-Sep-11 1:19am
   
Correct, my 5. OP needs to learn basics of .NET types. --SA
Mika Wendelius at 6-Sep-11 1:27am
   
Thanks, I agree
Member 7932936 at 6-Sep-11 1:20am
   
i already define datetime? dt=null; but it takes default value i.e 1/1/0001
Mika Wendelius at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

DateTime is not nullable type. You can use Nullable<datetime> dt;
  Permalink  
v2
Comments
Member 7932936 at 6-Sep-11 1:21am
   
I already used like that but it has set default datetime 1/1/0001
SAKryukov at 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
SAKryukov at 6-Sep-11 1:22am
   
Correct, my 5. I would prefer the simpler-to-use variant shown by Mika though, please see. --SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

we can't assign null value to datetime
 
because null value takes the system default date that is minimun date='1/1/0001'
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Null Value pass in datetime field
cmd1.Parameters.AddWithValue("@ups_delivery_date", SqlDbType.DateTime).Value = System.DBNull.Value;
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

dt = Convert.ToDateTime(strposteddate);
            strposteddate = dt.ToShortDateString();
            if (strposteddate ==null)
            {
              //allow something
            } 
            else
               
              // allow something
  Permalink  
v2
Comments
Member 7932936 at 6-Sep-11 1:26am
   
if null my datetime set to 1/1/0001 but hasn't takes null value
archanakumari at 6-Sep-11 1:36am
   
i have not read the question Properly
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

use
 
new Nullable <DateTime>
 
instead of just null
  Permalink  
Comments
ThePhantomUpvoter at 21-Jun-13 6:42am
   
Why is your solution to the ancient over the hill question any better that the other solutions already presented? Especially when this solution has already been posted... almost 2 years ago.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 Tadit Dash 330
2 Manas Bhardwaj 319
3 OriginalGriff 243
4 Sergey Alexandrovich Kryukov 205
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,716
2 Peter Leow 4,345
3 Maciej Los 3,540
4 Abhinav S 3,373


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 21 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid