在ASP.NET中将String转换为DateTime [英] Convert String to DateTime in ASP.NET
本文介绍了在ASP.NET中将String转换为DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai,
我需要将字符串转换为datetime。我使用过:
DateTime date = Convert.ToDateTime(txtdate.Text);
我需要从约会那个月开始。
但是我得到了错误:
字符串未被识别为有效日期时间
任何人都可以告诉解决方案。谢谢提前
Hai,
I need to convert string to datetime.I have used:
DateTime date = Convert.ToDateTime(txtdate.Text);
I need to take the month from the date.
But i Got the error:
String was not recognized as a valid DateTime
Can any one pls tell the solution.Thanks in Advance
推荐答案
嗯,这意味着该字符串无法解析为System.DateTime
。您需要指定预期的格式。正确的方法是:
http: //msdn.microsoft.com/en-us/library/ch92fbc1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/9h21f14e.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ms131044.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/h9b85w22.aspx [ ^ ]。
简而言之:您要么创建CultureInfo
的实例,要用作IFormatProvider
(由CultureInfo
实现)或使用格式字符串显式指定确切格式。您可以在上面引用的MSDN文章中找到它。
-SA
Well, it means the string cannot be parsed asSystem.DateTime
. You need to specify expected format. The right approach is this:
http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx[^],
http://msdn.microsoft.com/en-us/library/9h21f14e.aspx[^],
http://msdn.microsoft.com/en-us/library/ms131044.aspx[^],
http://msdn.microsoft.com/en-us/library/h9b85w22.aspx[^].
In a nutshell: you either create and instance ofCultureInfo
to use asIFormatProvider
(which is implemented byCultureInfo
) or explicitly specify exact format using a format string. You will find it all from the MSDN articles referenced above.
—SA
检查txtdate .Text格式,它是一种有效的C#数据时格式。
你可以使用
Check txtdate.Text format,Is it a valid C# datatime format.
and you can use as
DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
从日期获取月份值
您可以使用文本的SubString属性。
即如果文本的格式为dd-mm-yyyy
txtdate.Text.SubString(2,2)
将为您提供月份。
快乐编程: - )
To take month value from the date
You can use SubString property of the text.
ie if text is in the format dd-mm-yyyy the
txtdate.Text.SubString(2,2)
will give you the Month.
Happy Programming:-)
这篇关于在ASP.NET中将String转换为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文