string represents date, and reformat it

前端 未结 6 828
情书的邮戳
情书的邮戳 2020-12-21 17:45

The following case: There is a string that has this format \"2012-02-25 07:53:04\"

But in the end, i rather want to end up with this format \"25-02-2012 07:53:04\"

6条回答
  •  醉梦人生
    2020-12-21 18:06

    Do this:

    DateTime.Parse("2012-02-25 07:53:04").ToString("dd-MM-yyyy hh:mm:ss");
    

    Keep in mind this isn't culture-aware. And if you do need to store the intermediate result you could do that just as easily:

    var myDate = DateTime.Parse("2012-02-25 07:53:04");
    var myDateFormatted = myDate.ToString("dd-MM-yyyy hh:mm:ss");
    

    Lastly, check out TryParse() if you can't guarantee the input format will always be valid.

提交回复
热议问题