Remove hours:seconds:milliseconds in DateTime object

前端 未结 9 2117
忘了有多久
忘了有多久 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条回答
  • Usually, I am using DateTime.ToShortDateString() to convert in a Culture-aware manner to a string.

    This way, you can format it to date only respecting the current formatting of the culture set to the current thread.

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

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

    Something like this for date and time without seconds:

    private DateTime _startTime;
    public DateTime StartTime 
    { 
        get { return _startTime; } 
        set 
        { 
            _startTime = value.AddSeconds(-value.Second)
                              .AddMilliseconds(-value.Millisecond);   
        }
    }
    
    0 讨论(0)
提交回复
热议问题