c# - FormatException when formatting TimeSpan number of days without leading zeroes -
according msdn, d
format specifier outputs timespan.days property. when used in .tostring()
method appears true:
timespan.fromdays(1).tostring("%d")
however, when used in string.format
, specifier throws exception:
string.format("{0:d}", timespan.fromdays(1)) 'string.format("{0:d}", timespan.fromdays(1))' threw exception of type 'system.formatexception' base {system.systemexception}: {"input string not in correct format."}
the dd
specifier works fine, gives leading 0 (as intended).
why d
specifier throw exception?
you missing %
:
string.format("{0:%d}", timespan.fromdays(1))
according article linked (and example copied):
if "d" custom format specifier used alone, specify "%d" not misinterpreted standard format string.
Comments
Post a Comment