CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2007
    Posts
    19

    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.

  2. #2
    Join Date
    Jan 2007
    Posts
    19

    Re: converting nullable DateTime to non-null

    forgot to add revDate is passed in as a parameter and contains a date value.

  3. #3
    Join Date
    Dec 2006
    Posts
    203

    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

  4. #4
    Join Date
    Jan 2007
    Posts
    491

    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...

  5. #5
    Join Date
    Jan 2007
    Posts
    19

    Re: converting nullable DateTime to non-null

    Quote 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.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    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.

  7. #7
    Join Date
    Jan 2007
    Posts
    19

    Re: converting nullable DateTime to non-null

    Quote 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.

  8. #8
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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).

  9. #9
    Join Date
    Aug 2018
    Posts
    1

    Smile 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

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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;
    }

  11. #11
    Join Date
    Sep 2018
    Posts
    9

    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
  •  





Click Here to Expand Forum to Full Width

Featured