DateTime ToString(“dd/MM/yyyy”) returns dd.MM.yyyy

前端 未结 4 2078
刺人心
刺人心 2020-12-25 08:12

I have also tried shielding the \'/\' symbol in the formatting string, but it didn\'t quite work. My final goal is to get the date with the \'/\' symbols as separators. I g

4条回答
  •  青春惊慌失措
    2020-12-25 08:54

    The / character in date/time format strings stands for "whatever the date separator of the format provider is". Since you do not supply a format provider Thread.CurrentCulture is used, and in your case the current culture uses . as the date separator.

    If you want to use a literal slash, place it inside single quotes:

    dateTime.ToString("dd'/'MM'/'yyyy");
    

    Alternatively, you could specify a format provider where the date separator is /:

    dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
    

    All of the above is documented on MSDN.

    See the difference in a live example.

提交回复
热议问题