Remove hours:seconds:milliseconds in DateTime object

前端 未结 9 2116
忘了有多久
忘了有多久 2021-01-17 09:43

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

Conventionally the DateTime object comes as

相关标签:
9条回答
  • 2021-01-17 10:13

    About DateTime.AddMinutes (or seconds or hours) if you want to REMOVE instead of adding just add a negative number!

    0 讨论(0)
  • 2021-01-17 10:14

    Easy way, probably not the best but

    DateTime dt = new DateTime();
    dt = DateTime.Now;
    string sdate = dt.ToShortDateString();
    dt = DateTime.Parse(sdate);
    

    Or for short

    var dt = DateTime.Parse(DateTime.Now.ToShortDateString());
    
    0 讨论(0)
  • 2021-01-17 10:18

    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.

    0 讨论(0)
  • 2021-01-17 10:26

    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.

    0 讨论(0)
  • 2021-01-17 10:32

    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;
    

    The example below uses the Date property to extract the date component of a DateTime value with its time component set to zero (or 0:00:00, or midnight).

    msdn

    0 讨论(0)
  • 2021-01-17 10:33
    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

    0 讨论(0)
提交回复
热议问题