S.dob = txtdob.text无法将类型字符串隐式转换为system.datetime [英] S.dob=txtdob.text cannot implicitly convert type string to system.datetime

查看:94
本文介绍了S.dob = txtdob.text无法将类型字符串隐式转换为system.datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但为什么我没有转换。到日期选项



我尝试了什么:



But why I have not convert.to date option

What I have tried:

But why I have not convert.to date option 

推荐答案

阅读错误消息:

Read the error message:
cannot implicitly convert type string to system.datetime



这是因为C#是强类型的:它不会自动在数据类型之间进行转换(除非有一个特定的隐式转换方法被定义为类的一部分,例如从int到double)。

没有隐式转换来自字符串到DateTime,因为有很多不同的方式可以将日期写成字符串:


That is because C# is strongly typed: it will not automatically do converts between datatypes (unless there is a specific implicit conversion method defined as part of the class, as there is from int to double for example).
There is no implicit conversion from a string to a DateTime, because there are so many different ways that a Date can be written as a string:

2018-01-05
18-01-05
05-01-18
01-05-18

所有有效的方式代表今天的各种文化日期:UTC长/日本,UTC短片,欧洲,美国。

而且你不想转换它 - 你想解析输入并转换它是否有效:

Are all valid ways to represent today's date in various cultures: UTC long / Japan, UTC short, Europe, America.
And you don;t want to convert it anyway - you want to parse the input and convert it if it's valid:

DateTime dt;
if (!DateTime.TryParse(myStringContainingADate, out dt))
   {
   // There is a problem, it's not a valid date and / or time
   ... report problem to user ...
   return;
   }
// dt contains a valid DatetIme as entered here

它使用代码执行的PC的默认文化,如果用户在该计算机上键入,这可能是正确的。如果不是,则需要使用TryParseExact并匹配用户可能使用的文化。

That uses the default culture for the PC the code is executing on, which will probably be correct if the user is typing on that computer. If he isn't, you need to use TryParseExact and match the culture that the user is likely to use instead.


这篇关于S.dob = txtdob.text无法将类型字符串隐式转换为system.datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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