字符串未被识别为有效的 DateTime "格式 dd/MM/yyyy" [英] String was not recognized as a valid DateTime " format dd/MM/yyyy"
问题描述
我正在尝试将我的字符串格式值转换为格式为 dd/MM/yyyy
的日期类型.
I am trying to convert my string formatted value to date type with format dd/MM/yyyy
.
this.Text="22/11/2009";
DateTime date = DateTime.Parse(this.Text);
有什么问题?它有第二个覆盖要求 IFormatProvider
.这是什么?我也需要通过这个吗?如果是,如何在这种情况下使用它?
What is the problem ?
It has a second override which asks for IFormatProvider
. What is this? Do I need to pass this also? If Yes how to use it for this case?
编辑
Parse
和 ParseExact
有什么区别?
编辑 2
Slaks 和 Sam 的两个答案都对我有用,目前用户正在提供输入,但我可以确保它们通过使用 maskTextbox 是有效的.
Both answers of Slaks and Sam are working for me, currently user is giving the input but this will be assured by me that they are valid by using maskTextbox.
考虑到类型安全、性能或您喜欢的东西等所有方面,哪个答案更好
Which answer is better considering all aspects like type saftey, performance or something you feel like
推荐答案
使用 DateTime.ParseExact
.
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);
这篇关于字符串未被识别为有效的 DateTime "格式 dd/MM/yyyy"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!