c# - Different behaviour between formatting strings for TimeSpan and DateTime -
While coding today, I saw something odd with timepoints and formatting strings. I was trying to print again and again, for example 01:03:37
as 1:03:37
(without leading 0 for hours). So I used the format string h: mm: ss
. This, however, gave me a major 0. If I changed the Timespain to a date time and did the same again, then the h
formatting string was a hope for me as a work.
A sample console program:
class program {static zero main (string [] args) {var time = new timespan (01, 03, 37); Var Culture = New CultureInfo ("SV-SE"); Thread Present. Culture = culture agriculture = culture; Thread Existing content Current uk agriculture = culture; Console.WriteLine (time.ToString ()); Console.lightline (string format (culture, "{0: h: mm: ss}", time)); Console.light line (string format (culture, "{0: hh: mm: ss}", time)); Console.lightline ((new datetime (timetix) toasting ("H: MM: SS", culture)); Console.lightline ((new datetime (timetix)) toasting ("hh: mm: ss", culture)); Console.ReadKey (); }}
Output:
01:03:37 01:03:37 // & lt; - Expected: 1:03:37 01: 03:37 1:03:37 01:03:37
Why do timspines and datetime differ in different ways?
Because your formatting string timspain
and TimeSpan.ToString ()
does not work forever ():
A string that represents the value of this example. Return value is of the form:
[-] [D] hh: mm: ss [.ff]
Comments
Post a Comment