c# - DateTime: how to display as DD.MM.YYYY? -
i've got datetime variable , want convert string "dd.mm.yyyy" please note, values must separated "dot" sign.
of course can manual string composition. wonder if can use datetime.tostring()
required conversion.
yes, can:
string formatted = dt.tostring("dd'.'mm'.'yyyy");
now in case quotes aren't required, custom date/time format strings don't interpret dot in special way. however, make explicit - if change '.' ':' example, while it's quoted stay explicit character, unquoted "the culture-specific time separator". wasn't entirely obvious me whether "." interpreted "the culture-specific decimal separator" or not, hence quoting. may feel that's on top, of course - it's entirely decision.
you may want specify invariant culture, remove other traces of doubt:
string formatted = dt.tostring("dd'.'mm'.'yyyy", cultureinfo.invariantculture);
(at point quotes around dot become less relevant, "." decimal separator in invariant culture anyway.)
Comments
Post a Comment