如何将 dd/MM/yyyy 字符串转换为 MM-dd-YYYY DateTime int C#

How to convert dd/MM/yyyy string to MM-dd-YYYY DateTime int C#

在 C# 中将字符串 (dd/MM/yyyy) 转换为日期 (MM-dd-YYYY) 的最佳方法是什么?

string date = "15/01/2017";
DateTime date1 = DateTime.Parse(date, new CultureInfo("en-CA"));
btnBack.Text = date1.ToString();

我有错误

String was not recognized as a valid DateTime.

我建议你在你的情况下使用显式格式,你可以通过使用 ParseExact 来获取 DateTime 对象,然后为 ToString 重载提供所需的格式:

string date = "15/01/2017";
DateTime date1 = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.CurrentCulture);
btnBack.Text = date1.ToString("MM-dd-yyyy");