如何将文本框文本转换为datetime数据类型 [英] How to convert textbox text to datetime datatype

查看:66
本文介绍了如何将文本框文本转换为datetime数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有 textbox 作为用户可以输入时间的时间。

在数据库中,我将数据类型的时间声明为datetime。

它在转换方法中显示异常。

以下是我的代码...

<前lang =c#> ChemistPractiseTiming = Convert.ToDateTime(txtShopTiming.Text);

解决方案

如果只是你想转换的时间,那么使用DateTime.ParseExact或DateTime.TryParseExact,格式字符串只指定时间部分:

  string  inp =   14:07; 
DateTime dt = DateTime.ParseExact(inp, HH:mm,CultureInfo.InvariantCulture );


In my application, I have textbox as time for which user can enter time.
In database, I declared data type of time as datetime.
It showing me exception in convert method.
Below is my code...

ChemistPractiseTiming = Convert.ToDateTime(txtShopTiming.Text);

解决方案

If it is a time only you want to convert, then use DateTime.ParseExact, or DateTime.TryParseExact with a format string specifying only the time portion:

string inp = "14:07";
DateTime dt = DateTime.ParseExact(inp, "HH:mm", CultureInfo.InvariantCulture);


这篇关于如何将文本框文本转换为datetime数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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