10

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
Daniel
  • 9,491
  • 12
  • 50
  • 66
Kate
  • 935
  • 4
  • 14
  • 34
  • What do you mean "look like"? Do you mean when converting it to a string? Or what? – Ben Mar 02 '16 at 14:55
  • When you say the result of ts is 01:38:07.0093844, you mean after ToString()? I mean, you want to strip milliseconds of timespan or to show it as a string? – Pikoh Mar 02 '16 at 14:55
  • You should use formatting it it is only for display purposes, please check the MSDN documentation page : https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx – Irwene Mar 02 '16 at 14:55
  • Are you talking about string formatting? https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx – enkryptor Mar 02 '16 at 14:55
  • 1
    or DateTime.Now.ToShortTimeString() or ShortDateString / or format – Cadburry Mar 02 '16 at 14:55
  • If you want to result just *looks like* then use formatting when representing the `TimeSpan` as a `String` – Dmitry Bychenko Mar 02 '16 at 14:55
  • @ Ben . yes after ToSting(); – Kate Mar 02 '16 at 14:57
  • @ Pikoh . yes please. – Kate Mar 02 '16 at 14:58
  • @kate, you've got now a lot of answers below – Pikoh Mar 02 '16 at 14:59
  • @Kate look at the Extension method, easy to reuse again – Donald Jansen Mar 02 '16 at 15:01
  • this is the correct answer: TimeSpan trimmedTimeNow = new TimeSpan(ts.Days, ts.Hours, ts.Minutes, ts.Seconds); it gives you the day difference too – Kate Mar 02 '16 at 16:10
  • Use substring method and get only 8 chars `(yourTimeSpan).ToString().Substring(0,8)`. I found this much cleaner in the code than the weird format thats required to output this using TimeSpan. – GER Jan 23 '19 at 15:43

4 Answers4

18

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");
Tom
  • 2,360
  • 21
  • 15
  • 4
    You are assuming he knows how to use Extension Methods, add an example how it will work – Donald Jansen Mar 02 '16 at 14:56
  • still did not work for me. the Subtract() does not have function called StripMillisecond . anyway thank you for your help :) – Kate Mar 02 '16 at 16:07
  • 2
    StripMilliseconds is an extension method you need to declare in your code (see my first code snippet). More about extension methods -- https://msdn.microsoft.com/en-us/library/bb383977.aspx – Tom Mar 02 '16 at 16:14
5

What the object contains and what you want on the screen are separate concerns, do not mix the 2. If you want it formatted on the screen as hourse, minutes, seconds then use ToString() and include that in your format. Example:

var forScreen = ts.ToString("hh:mm:ss");

See all the formatting options available on MSDN Custom TimeSpan Format Strings.


Edit

As mentioned, you can make it whatever you want. Here is an example of ToString which builds out a human readable string. These formatters are meant to build a string that you can display so you do not have to actually make changes to the underlying data. This is your presentation logic.

dif.ToString("'Elapsed: 'dd' days, 'hh' hours, 'mm' minutes and 'ss' seconds'")
Igor
  • 60,821
  • 10
  • 100
  • 175
  • ok, but will this work if the result has days? like 3 days 2h 23mm 12ss – Kate Mar 02 '16 at 15:01
  • 1
    @Kate no it won't, if you want to have a specific formatting please read one of the two documentation links so that you can make your own – Irwene Mar 02 '16 at 15:03
  • @Kate - sure, you can use `ToString` to do whatever you want. As Sidewinder mentioned, figure out how you want to display it to the user, read the documentation, and create your own format string. – Igor Mar 02 '16 at 15:14
4

You can round through a division and a multiplication by the number of Ticks per second:

ts = new TimeSpan(ts.Ticks / TimeSpan.TicksPerSecond * TimeSpan.TicksPerSecond);

Internally a TimeSpan is "simply" a number of Ticks. By doing an integer division and an integer multiplication you can "round" them.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • @ xanatos. I used your code but the millisecond still there they set to 00000. any idea? tnx – Kate Mar 02 '16 at 15:12
1

You can just format the time like below:

string NewDateTime = ts.ToString("yyyy-MM-dd hh:mm:ss");
Ben Clarke
  • 1,051
  • 4
  • 21
  • 47