Convert Date String to another Date string with different format

前端 未结 4 1448
不思量自难忘°
不思量自难忘° 2021-01-18 21:45

I need to convert an string date with format yyyyMMdd to a date string with format MM/dd/yyyy. Which is the best to do it?

I\'m doing this:

4条回答
  •  旧时难觅i
    2021-01-18 22:33

    Your way is totally OK.You may try doing this:-

    string res = "20130908";
    DateTime d = DateTime.ParseExact(res, "yyyyMMdd", CultureInfo.InvariantCulture);
    Console.WriteLine(d.ToString("MM/dd/yyyy"));
    

    Just for reference from MSDN:-

    The DateTime.ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the format parameter.

提交回复
热议问题