在日期时间内转换文本框值 [英] coverting textbox value in the date time

查看:74
本文介绍了在日期时间内转换文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Convert.ToDateTime(txtdob.Text,dd / mm / yyyy)

i得到一个例外这个



字符串是不被认可为有效的DateTime。



plz帮助我

解决方案





你可以使用 DateTime.TryParse 功能。



参考 DateTime.TryParse方法 [ ^ ]



希望它有所帮助。


  var  date = DateTime.MinValue; 
var dateStr = string .Empty;
if (DateTime.TryParse(txtMyDate.Text, out date))
{
dateStr = date.ToString( dd / MM / yyyy);
}





但正如ratnakargudipati所说,它取决于系统日期时间格式。


如果您的文本框采用dd / MM / yyyy格式的日期,则以下代码会将其转换为有效的日期时间值:

  string  InputDate = YourDateTextBoxId.Text.Trim(); 
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = dd / MM / yyyy;
DateTime OutputDate = Convert.ToDateTime(InputDate,dateInfo);


Convert.ToDateTime(txtdob.Text,"dd/mm/yyyy")
i get an exception of this

String was not recognized as a valid DateTime.

plz help me

解决方案

Hi,

you can make use of DateTime.TryParse function.

refer DateTime.TryParse Method [^]

hope it helps.


var date=DateTime.MinValue;
var dateStr=string.Empty;
if(DateTime.TryParse(txtMyDate.Text, out date))
{
  dateStr=date.ToString("dd/MM/yyyy");
}



But as ratnakargudipati said, it is dependent on System Date time Format.


If your textbox takes date in dd/MM/yyyy format, the following code will convert it into valid datetime value:

string InputDate = YourDateTextBoxId.Text.Trim();
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = "dd/MM/yyyy";
DateTime OutputDate = Convert.ToDateTime(InputDate, dateInfo); 


这篇关于在日期时间内转换文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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