更改日期格式的字符串 [英] Changing string in date format
问题描述
大家好,
我有格式为24/01/2011的字符串,为日/月/年格式.
我想将其转换为日期时间格式.
在asp.net中,我做了这样的事情:
DateTime Dob = Convert.ToDateTime(TextBox2.Text);
但是它显示为字符串,而不是日期时间格式.
现在我在前端给出完整的代码,我做了一些事情,
<asp:TextBox ID="TextBox2" runat="server"> <cc1:CalendarExtender ID="TextBox2_CalendarExtender" runat="server" Enabled="True" Format="dd/MM/yyyy" TargetControlID="TextBox2"> <asp:RegularExpressionValidator ID="RXE" ControlToValidate="TextBox2" ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d" runat="server" ErrorMessage="RegularExpressionValidator"> <br /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server" Text="Label">
并在后面的代码中
如
DateTime Dob = DateTime.Parse(TextBox2.Text); Label1.Text = Dob.ToString();
错误为
无法将字符串识别为有效的DateTime.
请帮助我解决问题.
我的问题仅在2010年1月24日出现.
谢谢你
Mohammad wasif
如果运行代码,您将给出:
DateTime Dob = Convert.ToDateTime(TextBox2.Text);然后"Dob"将包含DateTime格式的日期-它不能容纳其他任何内容.
但是,每当您尝试将其打印到:控制台,标签,文本框,甚至在调试器中查看它时,都会隐式调用ToString
方法以使其起作用.
它仍然存储在DateTime中:尝试将"Dob"替换为"Dob.Month",您会看到仅显示月份.
请参阅此 ^ ]
>
Hi All,
I have string in format 24/01/2011 in day/month/year format.
I want to convert it into date time format.
In asp.net I did something like this:
DateTime Dob = Convert.ToDateTime(TextBox2.Text);
but it''s showing as string is not in date time format.
Now I am giving complete code in front end I did something as
<asp:TextBox ID="TextBox2" runat="server"> <cc1:CalendarExtender ID="TextBox2_CalendarExtender" runat="server" Enabled="True" Format="dd/MM/yyyy" TargetControlID="TextBox2"> <asp:RegularExpressionValidator ID="RXE" ControlToValidate="TextBox2" ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d" runat="server" ErrorMessage="RegularExpressionValidator"> <br /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server" Text="Label">
and in code behind
as
DateTime Dob = DateTime.Parse(TextBox2.Text); Label1.Text = Dob.ToString();
and the error is as
String was not recognized as a valid DateTime.
Please help me sort out the issue.
My problem only in 24/01/2010.
Thanking you
Mohammad wasif
If you run the code you give:
DateTime Dob = Convert.ToDateTime(TextBox2.Text);Then "Dob" will contain the date in DateTime format - it can''t hold anything else.
However, whenever you try to print it: to the console, or a label, or a textbox, or even view it in the debugger, there is an implicit call to theToString
method to make it work.
It is still stored in DateTime: Try replacing "Dob" with "Dob.Month" and you will see just the month appear.
Refer this [^]
这篇关于更改日期格式的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!