C#中的日期时间转换 [英] Datetime conversion in C#

查看:81
本文介绍了C#中的日期时间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试将字符串转换为日期时间但是它给出了错误。我的字符串格式是05/27/2016 8:00 AM想要转换日期时间。



DateTime dt = DateTime.ParseExact(05/27/2016 8:00 AM,dd / MM / yyyy,CultureInfo.InvariantCulture);



错误:字符串未被识别为有效的日期时间。



我尝试了不同的方式仍然显示相同的错误。请帮帮我。





先谢谢。



我尝试过:



DateTime dt = DateTime.ParseExact(05/27/2016 8:00 AM,dd / MM / yyyy,CultureInfo.InvariantCulture);

解决方案

这也有效:

 DateTime dt = DateTime.ParseExact(  05/27/2016 8:00 AM  MM / dd / yyyy H:mm tt,System.Globalization.CultureInfo.InvariantCulture); 





如需了解更多信息,请参阅: DateTime.ParseExact方法(字符串,字符串,IFormatProvider)(系统) [ ^ ]


试试这个



 DateTime dt1 = DateTime.ParseExact(  05/27 / 2016年8:00  MM / dd / yyyy h:mm tt ,CultureInfo.InvariantCulture); 





Quote:

dd =>第28天

MM =>第05个月

yyyy => 2016年

h =>小时8

mm =>分钟0

tt => AM / PM


 DateTime dt = DateTime.ParseExact(  05/27/2016 8:00 AM  dd / MM / yyyy,CultureInfo.InvariantCulture); 





可以映射05/27/2016 8:00 完全到dd / MM / yyyy?不,它不能因此错误。如果你想使用dd / MM / yyyy解析格式你的字符串必须匹配那个格式,所以从你的字符串中删除时间。



 DateTime dt = DateTime.ParseExact(  05/27/2016  dd / MM / yyyy,CultureInfo.InvariantCulture); 





那会有用吗?仍然没有。你说的是05是一天,27是月,没有第27个月。所以让你的日期字符串匹配这样的格式



 DateTime dt = DateTime.ParseExact(  05/27/2016  MM / dd / yyyy,CultureInfo.InvariantCulture); 







或使用正确的字符串格式,比如这个



 DateTime dt = DateTime.ParseExact(  05/27/2016 8:00 AM  MM / dd / yyyy h:mm tt,CultureInfo.InvariantCulture); 







自定义日期和时间格式字符串 [ ^ ]



如果是的话你要做的是创建一个DateTime对象,其中日期是一种特定的格式(你的格式字符串中的那个)然后日期不是这样的,它们没有格式,直到你将它们转换为串。因此,您将字符串转换为DateTime,并且DateTime表示该时刻,如果您希望以某种格式显示该时刻,则在DateTime上使用ToString并指定DateTime表示的格式。


Hi Guys,

I am trying to convert string to datetime but it's giving error. My string format is "05/27/2016 8:00 AM" want to convert in datetime.

DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "dd/MM/yyyy", CultureInfo.InvariantCulture);

Error: "String was not recognized as a valid DateTime."

I tried in different ways still showing same error. Please help me.


Thanks in Advance.

What I have tried:

DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "dd/MM/yyyy", CultureInfo.InvariantCulture);

解决方案

This is working as well:

DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "MM/dd/yyyy H:mm tt", System.Globalization.CultureInfo.InvariantCulture);



For further information, please see: DateTime.ParseExact Method (String, String, IFormatProvider) (System)[^]


try this

DateTime dt1 = DateTime.ParseExact("05/27/2016 8:00 AM", "MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture);



Quote:

dd => Day 28
MM => Month 05
yyyy=> Year 2016
h => hours 8
mm => minutes 0
tt => AM/PM


DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "dd/MM/yyyy", CultureInfo.InvariantCulture);



Can "05/27/2016 8:00" be mapped exactly to "dd/MM/yyyy"? No it can't hence the error. If you want to use the dd/MM/yyyy parse format your string has to match that format, so remove the time from your string.

DateTime dt = DateTime.ParseExact("05/27/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture);



Will that work? Still no. You are saying "05" is the day and "27" is the month, there is no 27th month. So make your date string match the format like this

DateTime dt = DateTime.ParseExact("05/27/2016", "MM/dd/yyyy", CultureInfo.InvariantCulture);




Or use the proper format for the string you have, like this

DateTime dt = DateTime.ParseExact("05/27/2016 8:00 AM", "MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture);




Custom Date and Time Format Strings[^]

If what you're trying to do is create a DateTime object where the date is a specific format (the one you have in your format string) then dates don't work like that, they don't have a format until you convert them to a string. So you convert your string to DateTime and that DateTime represents that moment in time, and if you want to display that moment in time in a certain format you use ToString on the DateTime and specify the format you what the DateTime represented as.


这篇关于C#中的日期时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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