覆盖字符串DateTime [英] Coverting string DateTime

查看:96
本文介绍了覆盖字符串DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我需要帮助来解决此问题.我正在尝试将字符串[4/20/2011 8:17:21 AM]转换为DateTime.为此,我尝试了以下方法:.

Dear Friend,

i need a help to solve this problem.I am trying to convert a string [4/20/2011 8:17:21 AM] to DateTime. For this i tried like the following ways..

string date = "4/20/2011 8:17";

Convert.ToDateTime(date);

and

DateTime.ParseExact(date,"M/dd/yyy",System.Globalization.CultureInfo.CurrentCulture);


但是我收到错误消息字符串未被识别为有效的DateTime."

请帮助我..


But i am getting error message "String was not recognized as a valid DateTime."

Please help me ..

推荐答案

对于Convert.ToDateTime调用,可以(或应该).对于ParseExact调用,您的格式字符串必须为:

"M/dd/yyyy h:mm"
For the Convert.ToDateTime call, it''s okay (or should be). For the ParseExact call, your format string needs to be this:

"M/dd/yyyy h:mm"


尝试一下,

try this,

string strDateTime = "2/28/1992 12:15:12";
DateTime myDateTime = DateTime.Parse(strDateTime );

Or

myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",System.Globalization.CultureInfo.InvariantCulture)


尝试使用System.Globalization

try with this using System.Globalization

CultureInfo provider = CultureInfo.InvariantCulture;
            string date = "04/20/2011 08:17";
            DateTime dt1 = DateTime.ParseExact(date, "g",provider);


这篇关于覆盖字符串DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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