Convert date in string to DateTime with same format

后端 未结 3 626
Happy的楠姐
Happy的楠姐 2021-01-20 21:02

I have a string that has a date stored in it.

String date = \"03-05-2013 00:00:00\";

I parsed it to Datetime as follows:

Da         


        
3条回答
  •  感情败类
    2021-01-20 21:33

    The format you parse with does not dictate how the DateTime is formatted when you convert the date back to a string. When you call ToString on a date it pulls the format from the current culture of the thread your code is executing on (which defaults to the culture of the machine your on).

    You can override this by passing the format into ToString() i.e.

    Start.ToString("dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
    

    See Custom Date and Time Formats.

提交回复
热议问题