I am new to c# and using windows forms. the result of this code is: 01:38:07.0093844
. Anyone knows how can I remove the millisecond part (0093844) from the result (ts) I want the result to look like this : 01:38:07
(H:mm:ss) without millisecond .
Please help .Thank you
string OldDateTime = "2016-03-02 13:00:00.597"; //old DateTime
DateTime CurrentDateTime = DateTime.Now;
TimeSpan ts = CurrentDateTime.Subtract(Convert.ToDateTime(OldDateTime)); //Difference
//result of ts = 01:38:07.0093844
Create an extension method:
public static class TimeExtensions
{
public static TimeSpan StripMilliseconds(this TimeSpan time)
{
return new TimeSpan(time.Days, time.Hours, time.Minutes, time.Seconds);
}
}
Usage:
string OldDateTime = "2016-03-02 13:00:00.597"; //old DateTime
DateTime CurrentDateTime = DateTime.Now;
TimeSpan ts = CurrentDateTime.Subtract(Convert.ToDateTime(OldDateTime)).StripMilliseconds();
To format (make into a string) without milliseconds use this:
string OldDateTime = "2016-03-02 13:00:00.597"; //old DateTime
DateTime CurrentDateTime = DateTime.Now;
TimeSpan ts = CurrentDateTime.Subtract(Convert.ToDateTime(OldDateTime));
string formatted = ts.ToString(@"dd\.hh\:mm\:ss");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With