转换数据时出错,错误是日期时间无效,我想要日期在2014/12/10 4:39这个格式 [英] Getting Error While Converting Datatime And Error Is Datetime Is Not Valid And I Want Date In 2014/12/10 4:39 Pm This Format

查看:56
本文介绍了转换数据时出错,错误是日期时间无效,我想要日期在2014/12/10 4:39这个格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是



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

推荐答案

这里有很多东西:

fl.CreationTime看起来像 FileSystemInfo.CreationTime FileInfo.CreationTime - 这是a DateTime 已经,所以你不需要转换它:

所以...

There are a number of things here:
fl.CreationTime looks like a FileSystemInfo.CreationTime or a FileInfo.CreationTime - which is a DateTime already, so you don;t need to convert it:
So...
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这个格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆