Asp.net中的DateTime问题 [英] DateTime Problem in Asp.net
问题描述
我有两个TextBox(txtdateofissue和txtdateofexpiry)。第一个文本框附带一个CalendarExtender控件。
如果用户选择txtdateofissue文本框的日期然后自动在同一日期我想要但是明年应该输入txtdateofexpiry文本框。
日期应该是dd-MM-yyyy格式,即24-04-2013。
i已为此编写代码但我收到错误(字符串不是有效的日期时间格式)
我的代码:
< pre lang =cs> protected void txtdateofIssue_TextChanged( object sender,EventArgs e)
{
DateTime dt = Convert.ToDateTime(txtdateofIssue.Text);
dt = dt.AddDays(-1).AddYears( 1 );
txtdateofExpiry.Text = dt.ToShortDateString();
}
我声明日期时间变量dt的第一行会引发错误(String不是有效的日期时间格式)
当我在我的本地机器上运行此代码时工作正常但如果我在iis上运行它然后显示错误...请更正我的代码或指南我以一种新的方式...提前感谢...
不要使用所有转换
方法,尽量避免使用它们。做你真正想做的事 - 解析。有两个原因:1)解析这个词可以帮助你理解你真正做的事情; 2)解析方法允许您根据您期望的字符串添加参数以指定字符串表示时间的格式/文化。
您需要使用结构中的一种方法System.DateTime
:解析
,TryParse
,ParseExact
或TryParseExact
。请参阅:
http://msdn.microsoft.com/en -us / library / system.datetime.aspx [ ^ ]。
格式说明符请参阅:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ]。
-SA
DateTime dt;
if (DateTime.TryParseExact(txtdateofIssue.Text, dd-MM-yyyy,CultureInfo.InvariantCulture,DateTimeStyles.None, out dt))
{
dt = dt.AddDays(-1).AddYears( 1 );
txtdateofExpiry.Text = dt.ToString( dd-MM-yyyy);
}
设置日历扩展器的Format =dd / MM / yyyy属性并更改你的textchanged事件如下
受保护 void txtdateofissue_OnTextChanged( object sender,EventArgs e)
{
string [] str = txtdateofissue。 Text.Trim()。Split(' /');
System.DateTime dt = new System.DateTime( int .Parse(str [ 2 ]), int .Parse(str [ 1 ]), int .Parse(str [ 0 ]));
dt = dt.AddDays(-1).AddYears( 1 );
txtdateofexpiry.Text = dt.ToShortDateString();
}
I have got two TextBoxes(txtdateofissue and txtdateofexpiry).The First Textbox is Attached with a CalendarExtender control.
I want that if a user selects a date for the txtdateofissue textbox then automatically the same date but next year should get entered in txtdateofexpiry textbox.
Date Should be in dd-MM-yyyy format i.e. 24-04-2013.
i have written a code for this but i am getting an error (String is not a valid datetime format)
my code:
protected void txtdateofIssue_TextChanged(object sender, EventArgs e)
{
DateTime dt = Convert.ToDateTime(txtdateofIssue.Text);
dt = dt.AddDays(-1).AddYears(1);
txtdateofExpiry.Text = dt.ToShortDateString();
}
the first line in which i declare datetime variable dt throws up the error (String is not a valid datetime format)
when i run this code in my local machine it works fine but if i run it on iis then it shows errors...please either correct my code or guide me in a new way...thanks in advance...
Don''t use all thoseConvert
methods, try to avoid them in general. Do what you really mean to do — parsing. By two reasons: 1) the word "parse" helps you to understand what you really do; 2) parsing methods allows you to add parameters to specify format/culture of the string presentation of time, according to the string you expect.
You need to use one of the methods of the structureSystem.DateTime
:Parse
,TryParse
,ParseExact
orTryParseExact
. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].
For format specifiers, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].
—SA
DateTime dt; if (DateTime.TryParseExact(txtdateofIssue.Text, "dd-MM-yyyy",CultureInfo.InvariantCulture,DateTimeStyles.None,out dt)) { dt = dt.AddDays(-1).AddYears(1); txtdateofExpiry.Text = dt.ToString("dd-MM-yyyy"); }
set Format="dd/MM/yyyy" attribute for calendar extender and change ur textchanged event as follows
protected void txtdateofissue_OnTextChanged(object sender, EventArgs e) { string[] str = txtdateofissue.Text.Trim().Split('/'); System.DateTime dt = new System.DateTime(int.Parse(str[2]), int.Parse(str[1]), int.Parse(str[0])); dt = dt.AddDays(-1).AddYears(1); txtdateofexpiry.Text = dt.ToShortDateString(); }
这篇关于Asp.net中的DateTime问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!