如何将文本框值转换为时间?
我想通过减去当前时间和文本框时间来将我的文本框值转换为时间,并计算所花费的时间。但是,在输入文本框的时间后,发生了一个例外。
这是我遇到错误的代码;
system.formatexception:'字符串未被识别为有效
DateTime timeValue = DateTime.ParseExact(txtTime.Text, "M/d/yyyy", CultureInfo.InvariantCulture);
我
DateTime timeValue = Convert.ToDateTime(txtTime.Text);
也尝试过,但它不起作用。谁能帮我吗?
I want to convert my textbox value to the time and calculate the hours spent by subtracting current time and textbox time. But after entering the time for textbox, an exception occurred.
This is my code where I'm getting an error;
System.FormatException: 'String was not recognized as a valid
DateTime timeValue = DateTime.ParseExact(txtTime.Text, "M/d/yyyy", CultureInfo.InvariantCulture);
I tried
DateTime timeValue = Convert.ToDateTime(txtTime.Text);
also but it didn't work. Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
HH:MM
而不是m/d/yyyy
在解析用户输入时仅使用dateTime.tryparseexact获得时间。注释上面的时间为24小时,例如输入01:00是1:00 AM 13:00是1:00 pm
You need to use
HH:mm
rather thanM/d/yyyy
when parsing user input to obtain time only using DateTime.TryParseExact.Note the above is 24 hour time e.g. enter 01:00 is 1:00 AM while entering 13:00 is 1:00 PM