-
June 6th, 2007, 04:06 PM
#1
converting nullable DateTime to non-null
I'm trying to convert nullable DateTime to non-nullable DateTime with the following code:
DateTime? revDate;
NullableConverter nc = new NullableConverter(typeof(DateTime?));
DateTime NonNullRevDate = (DateTime) nc.UnderlyingTypeConverter.ConvertTo(revDate, nc.UnderlyingType);
but this is not working. What is wrong with it? It is throwing an exception.
-
June 6th, 2007, 04:27 PM
#2
Re: converting nullable DateTime to non-null
forgot to add revDate is passed in as a parameter and contains a date value.
-
June 6th, 2007, 04:34 PM
#3
Re: converting nullable DateTime to non-null
I'm not an expert on this, but isn't using revDate.Value what you're looking for?
Sincerely,
Martin Svendsen
-
June 6th, 2007, 05:03 PM
#4
Re: converting nullable DateTime to non-null
What's wrong about this:
Code:
DateTime? dtBefore = null;
dtBefore = DateTime.Now;
DateTime dtAfter = (DataTime)dtBefore;
It has to work. Try that out...
-
June 7th, 2007, 08:07 AM
#5
Re: converting nullable DateTime to non-null
Originally Posted by Talikag
What's wrong about this:
Code:
DateTime? dtBefore = null;
dtBefore = DateTime.Now;
DateTime dtAfter = (DataTime)dtBefore;
It has to work. Try that out...
it doesn't even compile.
you cannot convert a nullable type to a non-nullable type.
-
June 7th, 2007, 08:10 AM
#6
Re: converting nullable DateTime to non-null
It does compile and it does work.
Code:
using System;
class Test {
public static void Main ()
{
DateTime? dtBefore = null;
dtBefore = DateTime.Now;
DateTime dtAfter = (DateTime)dtBefore;
Console.WriteLine(dtBefore.HasValue);
Console.WriteLine(dtAfter);
try
{
dtBefore = null;
Console.WriteLine(dtBefore.HasValue);
dtAfter = (DateTime)dtBefore;
}
catch(InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
}
}
Last edited by Mutant_Fruit; June 7th, 2007 at 08:14 AM.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
June 7th, 2007, 08:28 AM
#7
Re: converting nullable DateTime to non-null
Originally Posted by Homogenn
I'm not an expert on this, but isn't using revDate.Value what you're looking for?
Thanks!
That's exactly what I needed.
Can't believe how simple that was.
-
June 7th, 2007, 08:51 AM
#8
Re: converting nullable DateTime to non-null
you get the value of nullable types by either its value property or by casting it to its non-nullable type (and yes, it does work).
-
August 25th, 2018, 12:54 AM
#9
Re: converting nullable DateTime to non-null
DateTime FilterSDate = DateTime.Now;
DateTime FilterEDate = DateTime.Now;
if(sdate!=null && edate!=null)
{
var StartDate = sdate.ToString();
FilterSDate = Convert.ToDateTime(StartDate);
var EndDate = edate.ToString();
FilterEDate = Convert.ToDateTime(EndDate);
}
And you can use the FilterSDate and FilterEDate finally
-
August 27th, 2018, 06:37 PM
#10
Re: converting nullable DateTime to non-null
Way old post. Now you can use the HasValue and Value properties.
Code:
if(nullableDateTime.HasValue)
{
var dt = nullableDateTime.Value;
}
-
September 17th, 2018, 12:11 AM
#11
Re: converting nullable DateTime to non-null
You can't convert nullable to non nullable value.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|