Visual C# Developer Center > Visual C# Forums > Visual C# General > Remove time from DateTime.MinValue
Ask a questionAsk a question
 

AnswerRemove time from DateTime.MinValue

  • Friday, November 06, 2009 4:53 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    this has got to simple stupid but I can't get this to work by using ToString.  How do you remove time from a MinValue?

    This syntax throws a compile error: someDate = DateTime.MinValue.ToString("dd/MM/yyyy");

    C# Web Developer
    • Edited byNoEgo Friday, November 06, 2009 4:56 PM
    •  

Answers

  • Friday, November 06, 2009 5:01 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The only way to do that is to use the Date data type in SQL.  In .NET you have no choice.  There's always going to be a time attached.  Period.  You can set it to midnight or whatever, but it's still there.  When you send a DateTime to SQL to update a Date type column, it'll automatically ignore the time portion of the struct and just save the date.

    In other words, you shouldn't have to do anything here to make it work.  It's already done for you.

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked As Answer byNoEgo Friday, November 06, 2009 6:00 PM
    •  

All Replies

  • Friday, November 06, 2009 4:55 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    There are several methods in DateTime that all start with "To". 

    Like:

    ToLongDateString

    and

    ToShortDateString

    :)

    When in doubt, always scan what intellisense gives you.

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Friday, November 06, 2009 4:56 PMBigTuna99 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    DateTime.MinValue.ToString("MM/dd/yyyy")



    If this answers your question, please mark the question as answered.
  • Friday, November 06, 2009 4:59 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Ok here's my problem:

    DateTime birthDate;

    if (some check here)
                    birthDate = DateTime.MinValue.ToShortDateString();
                else
    ...
    birthDate = new DateTime(year, month, day);

    if I want to get rid of the time on birthDate I'll have to make the birthDate variable a string.  Ultimately I want to set someObject.BirthDate which is of type Date to this string...how would I do this if my date is no longer a DateTime but the property I want to set in the object expects a DateTime?

    Ultimately my end goal is to send down MinDate to the new SQL 2008 Date field which does not hold time, just date.
    C# Web Developer
  • Friday, November 06, 2009 5:01 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The only way to do that is to use the Date data type in SQL.  In .NET you have no choice.  There's always going to be a time attached.  Period.  You can set it to midnight or whatever, but it's still there.  When you send a DateTime to SQL to update a Date type column, it'll automatically ignore the time portion of the struct and just save the date.

    In other words, you shouldn't have to do anything here to make it work.  It's already done for you.

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked As Answer byNoEgo Friday, November 06, 2009 6:00 PM
    •  
  • Friday, November 06, 2009 5:03 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    You can use Date property of a datetime object to get rid of time.

    Ex : DateTime.Now.Date.
  • Friday, November 06, 2009 5:27 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    yea, I don't want Now...I knew about that but we are not setting the time to now.
    C# Web Developer
  • Friday, November 06, 2009 5:31 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    He was saying that the .Date property will remove the time from the DateTime, regardless of whether you use "birthDate.Date" or "DateTime.Now.Date"

    Nevertheless, that isn't completely true.  It doesn't remove the time portion, it merely sets it to midnight.  The time is still there. 

    Again, NoEgo, you shouldn't worry about it.  If your column is of type Date, then SQL will automatically ignore the time portion for you.  You only need to remove the time when formatting for the user. 
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Friday, November 06, 2009 6:00 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yea, I know Dave. I was just saying I'm not using Now, and that I knew about Now.Date.  That didn't help me in this scenario since I'm not using Now
    C# Web Developer