将datetime转换为yyyy / MM / dd [英] convert datetime to yyyy/MM/dd
问题描述
如何将datetime对象转换为yyyy / MM / dd格式?我正在尝试:
How can I convert a datetime object to the format yyyy/MM/dd? I am trying this:
DateTime selectedDate =Convert.ToDateTime( Calendar1.SelectedDate.ToShortDateString());
selectedDate = DateTime.ParseExact(selectedDate, "yyyy/MM/dd", CultureInfo.InvariantCulture);
但是我收到以下错误:
最好的重载方法匹配'System.DateTime.ParseExact(string,string,System.IFormatProvider)'有一些无效的参数
The best overloaded method match for 'System.DateTime.ParseExact(string, string, System.IFormatProvider)' has some invalid arguments
或
参数'1':无法从System.DateTime转换为string
Argument '1': cannot convert from 'System.DateTime' to 'string'
推荐答案
A DateTime
没有固有的格式 - 只有内部表示。
A DateTime
does not have an inherent format - only an internal representation.
当您想要显示 / code>实例:
Formatting comes into play when you want to display the value of the DateTime
instance:
string formatted = DateTime.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
上述格式化的
字符串将包含值的 DateTime
实例。
The above formatted
string will contain the values of the DateTime
instance in the required format.
这篇关于将datetime转换为yyyy / MM / dd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!