问题描述
我的代码是
my code is
string s = Convert.ToDateTime(fl.CreationTime).ToString();
flist.UploadDate = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm tt", null).ToString();
i想要在2014/12/10 4:39 PM这个格式
i want date in 2014/12/10 4:39 PM this format
推荐答案
string s = Convert.ToDateTime(fl.CreationTime).ToString();
可以只是:
Can be just:
string s = fl.CreationTime.ToString();
其次,DateTime的默认ToString是使用当前的系统设置 - 这非常非常不可能是ISO格式。所以你生成的字符串将是
Secondly, the default ToString for a DateTime is to use the current system settings - which is very, very unlikely to be in ISO format. So the string you generate will be along the lines of
15/02/2001 17:45
这与你的格式字符串不匹配正在告诉ParseExact期待。因此ParseExact将失败,并抛出异常。 Yo可以为ToString提供一个参数来生成正确的格式: [] ...
但是......为什么你要解析该死的字符串呢? />
为什么不直接使用你开始使用的原始DateTime?
which will not match the format string you are telling ParseExact to expect. So ParseExact will fail, and throw an exception. Yo could provide a parameter to ToString to generate the correct format: Formatting a DateTime for display - format string description[^]...
But...why are you parsing the damn string anyway?
Why not just use the original DateTime that you started with?
flist.UploadDate = fl.CreationTime;
应该是你所需要的......
should be all you need...
这篇关于转换数据时出错,错误是日期时间无效,我想要日期在2014/12/10 4:39这个格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!