在c#中转换datetime时出错 [英] Error convert datetime in c#

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

问题描述

我有1个文本框,其中包含以下数据

I have 1 textbox that contains the following data

TextBox1.Text = "06/15/2013 22:22";



and

_cultureInfo CultureInfo = new CultureInfo ("en-US", false);



但我用


but I used

DateTime.ParseEtract (TextBox1.Text, "MM/dd/yyyy HH:mm", _cultureInfo);


转换后
错误:字符串未被识别为有效的DateTime。

我正在使用convertdate但结果:

25/05/2013 10:09:00 SA

我想要结果:05/25/2013 10:09:00 SA


error after convert: String was not recognized as a valid DateTime.
I am using convertdate but result :
25/05/2013 10:09:00 SA
I want result : 05/25/2013 10:09:00 SA

推荐答案

您没有发布源代码。你重新键入它,它有拼写错误的单词和额外的空格。这使得很难说出你在程序中实际拥有的内容。



此代码适用于我在Visual Studio 2012中使用C#:

You didn't publish your source code. You re-typed it and it has misspelled words and extra spaces. That makes it difficult to tell what you actually have in your program.

This code works for me using C# in Visual Studio 2012:
CultureInfo c = new CultureInfo("en-US", false);
String s = "06/15/2013 22:22";
DateTime r = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm", c);
Console.WriteLine(r.ToString("MM/dd/yyyy hh:mmtt",c))



输出:


Output:

引用:

06/15/2013 10:22 PM

06/15/2013 10:22PM





参见 DateTime.ToString方法(字符串,IFormatProvider) [ ^ ]


hello professional





此链接绝对可以帮到你...





字符串未被识别为有效的DateTime。 [ ^ ]





乐于帮助!!!
hello professional


this link definitely help you...


String was not recognized as a valid DateTime.[^]


happy to help!!!


DateTime.ParseEtract (TextBox1.Text, "MM / dd / yyyy HH: mm", _cultureInfo);





您的输入字符串在所有元素之间没有空格,那么为什么您的格式字符串?



应该是:





Your input string doesn't have spaces between all the elements, so why does your format string?

Should be:

DateTime.ParseEtract (TextBox1.Text, "MM/dd/yyyy HH:mm", _cultureInfo);





空格是很重要,特别是在使用DateTime.Parse时完全()


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

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