Convert Date String to another Date string with different format

前端 未结 4 1423
不思量自难忘°
不思量自难忘° 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条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-18 22:29

    Your code is in fact the best (of course better using TryParseExact), however looks like your input string is in the correct format (convertible to DateTime), you just want to re-format it, I think using some string method will help. But I would like to use Regex here:

    string output = Regex.Replace(input, "^(\\d{4})(\\d{2})(\\d{2})$", "$2/$3/$1");
    //E.g
    input = "20130920";
    output = "09/20/2013";
    

提交回复
热议问题