How to convert a date string “dd/MM/yyyy” format into “MM/dd/yyyy” in asp.net c#?

前端 未结 4 1399
自闭症患者
自闭症患者 2021-01-13 11:10

I want to convert a string date formate \"dd/MM/yyyy\" into \"MM/dd/yyyy\" in c# example

 string d =\"25/02/2012\";  i want to convert into 02/25/2012
         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-13 11:49

    You can parse it to DateTime object using DateTime.ParseExact and later use ToString("MM/dd/yyyy")to display theDateTime` object like.

    string d ="25/02/2012";
    DateTime dt = DateTime.ParseExact(d, "d/M/yyyy", CultureInfo.InvariantCulture);
    // for both "1/1/2000" or "25/1/2000" formats
    string newString = dt.ToString("MM/dd/yyyy");
    

    Make sure to include using System.Globalization; at the top.

提交回复
热议问题