Visual C# Developer Center >
Visual C# Forums
>
Visual C# General
>
Remove time from DateTime.MinValue
Remove time from DateTime.MinValue
- 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
- 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 Wiki • LinkedIn • ForumsBrowser- Marked As Answer byNoEgo Friday, November 06, 2009 6:00 PM
All Replies
- 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 Wiki • LinkedIn • ForumsBrowser- Proposed As Answer byDavid M MortonMVP, ModeratorFriday, November 06, 2009 4:57 PM
- DateTime.MinValue.ToString("MM/dd/yyyy")
If this answers your question, please mark the question as answered.- Proposed As Answer byDavid M MortonMVP, ModeratorFriday, November 06, 2009 4:57 PM
- 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 - 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 Wiki • LinkedIn • ForumsBrowser- Marked As Answer byNoEgo Friday, November 06, 2009 6:00 PM
- Hi,
You can use Date property of a datetime object to get rid of time.
Ex : DateTime.Now.Date. - yea, I don't want Now...I knew about that but we are not setting the time to now.
C# Web Developer - 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 Wiki • LinkedIn • ForumsBrowser - 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


