更改日期格式的字符串 [英] Changing string in date format

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

问题描述

大家好,

我有格式为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",您会看到仅显示月份.


请参阅此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 the ToString 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屋!

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