Remove hours:seconds:milliseconds in DateTime object

70,180

Solution 1

To answer your question, no - you would have to store it in a different type. The most simple choice is to use a string.

string date = dateTime.ToString("MM:dd:yyyy");

However I'd also strongly advise against storing dates internally in your program as strings. This will make it difficult to do any calculations or comparisons on them. Furthermore I'd advise you against forcing a specific culture for your date representation as it means your application probably won't work as expected in other cultures than yours.

A slightly more sophisticated approach is to create a custom class which overrides ToString. I'd also avoid this though, because it will still be difficult to use your type with the standard library functions. You will have to convert back and forth all the time.

Just leave it as a DateTime and do the conversion to string only in the presentation layer. You can use DateTime.ToShortDateStringto print a user friendly culture aware string.

Solution 2

While in most cases I agree with Mark Byers, I had a situation where I needed to store a date time that was only ever granular to the hour. Storing minutes and seconds would not only be superfluous, but also inaccurate. The user simply selected a date and hour, so while the date and hour would be user selected, the minutes and seconds would be set to whatever the current time was.

Removing minutes and seconds is very easy in this case. Here is the code:

scheduledDate = scheduledDate.AddMinutes(
    scheduledDate.Minute * -1).AddSeconds(
    scheduledDate.Second * -1);

Then I store it in the DB as a full date time, with minutes and seconds always 0.

Solution 3

If you want to remove hours, seconds, milliseconds (remove as reset to 0) from a DateTime object without converting to a string, you can use the Date property.

// Get date-only portion of date, without its time.
var date = DateTime.Now.Date;

msdn

Solution 4

datetime DateWithTimeNoSeconds = 
DateTime.Now.Date.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute);

This gets the current date & time's date and adds hours and minutes.

Solution 5

DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());

// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));   
// The example displays the following output to the console:
//       6/1/2008 7:47:00 AM
//       6/1/2008
//       6/1/2008 12:00 AM
//       06/01/2008 00:00

http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx

Share:
70,180
locoboy
Author by

locoboy

Love any kind of something that is about engineering

Updated on September 28, 2021

Comments

  • locoboy
    locoboy over 2 years

    I'm trying to create a string from the DateTime object which yields the format mm:dd:yyyy.

    Conventionally the DateTime object comes as mm:dd:yyyy hrs:min:sec AM/PM.

    Is there a way to quickly remove the hrs:min:sec AM/PM portion of the DateTime so that when I convert it ToString() it will only result in mm:dd:yyyy?

  • ShigaSuresh
    ShigaSuresh over 13 years
    The OP said he was creating a string, so I think you should have said Yes, in your answer :-D
  • Mark Byers
    Mark Byers over 13 years
    +1 for DateTime.ToShortDateString(). I was just writing that in my answer, and than afterwards noticed you also posted the exact same suggestion.
  • Uwe Keim
    Uwe Keim over 13 years
    Wow, a 153-er out-performed a 100379-er. Must be my lucky day :-). Thank you!
  • locoboy
    locoboy over 13 years
    I like this solution because I need it as a string when querying an outside webservice :)
  • Dave
    Dave about 12 years
    Don't try to convert a datetime to string to manipulate it! This is bad form and much slower than building the current date time from it's components.
  • Evgeniy Berezovsky
    Evgeniy Berezovsky over 11 years
    You should do DateTime.Now once only, save the result in a variable and use that variable, otherwise you risk constructing a DateWithTimeNoSeconds that is one hour off, or even one day off (if used at the turn of the hour / day). Example: var now = DateTime.Now; now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0);
  • But I'm Not A Wrapper Class
    But I'm Not A Wrapper Class over 7 years
    This is probably the best way to flatten date times.