Remove hours:seconds:milliseconds in DateTime object

前端 未结 9 2130
忘了有多久
忘了有多久 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: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.

提交回复
热议问题