Skip to main content
 首页 » 编程设计

c#之将日期时间转换为 yyyy/MM/dd

2024年02月23日16dudu

如何将日期时间对象转换为 yyyy/MM/dd 格式?我正在尝试这个:

DateTime selectedDate =Convert.ToDateTime( Calendar1.SelectedDate.ToShortDateString()); 
selectedDate = DateTime.ParseExact(selectedDate, "yyyy/MM/dd", CultureInfo.InvariantCulture); 

但是我得到以下错误:

The best overloaded method match for 'System.DateTime.ParseExact(string, string, System.IFormatProvider)' has some invalid arguments

或

Argument '1': cannot convert from 'System.DateTime' to 'string'

请您参考如下方法:

DateTime 没有固有格式 - 只有内部表示。

当您想要显示 DateTime 实例的值时,格式化就会发挥作用:

string formatted = DateTime.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture); 

上面的 formatted 字符串将包含所需格式的 DateTime 实例的值。