字符串未被识别为有效的 DateTime "格式 dd/MM/yyyy" [英] String was not recognized as a valid DateTime " format dd/MM/yyyy"

查看:21
本文介绍了字符串未被识别为有效的 DateTime "格式 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?

编辑

ParseParseExact 有什么区别?

编辑 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屋!

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