C # string converted to datetime? (String converted to nullable date type)

Source: Internet
Author: User

Reproduced

Ten has three

Source: HTTP://SHIYOUSAN.COM/POST/CA4A6413-ECB4-4237-BAF6-E88E616D18FC

PS: This article is mainly about the nullable date type and the conversion between the strings, normal type conversion look at this post: string type conversion to datetime type

A recent project that has been plagued with problems in the past is how to convert a string to a DateTime. This nullable date type. I used to write a bunch of logic code to convert, but it was tedious to write the code. After browsing the relevant information on the Internet, using the Nullableconverter class can be easily converted.

Here is some code for the test, which runs in the console application:

/* 测试string类型转换成DateTime?类型*/ /*NullableConverter类构造函数必须传入要转换的类型*/System.ComponentModel.NullableConverter nullableDateTime = new System.ComponentModel.NullableConverter(typeof(DateTime?));/**正常日期格式字符串转换为DateTime?*/string strDate = DateTime.Now.ToString();DateTime? dt1=(DateTime?)nullableDateTime.ConvertFromString(strDate);Console.WriteLine("正常日期格式字符串转换成DateTime?:{0}", dt1);/**字符串为空白转换为DateTime?*/strDate = string.Empty;DateTime? dt2 = (DateTime?)nullableDateTime.ConvertFromString(strDate);Console.WriteLine("空白字符串转换成DateTime?:{0}", dt2);/**字符串为NULL转换为DateTime?*/strDate = null;DateTime? dt3 = (DateTime?)nullableDateTime.ConvertFromString(strDate);Console.WriteLine("NULL字符串转换成DateTime?:{0}", dt3); Console.Read();

The result of the output is:

Normal date format string converted to DATETIME?:2013/11/5 15:49:16

Blank string converted to datetime?;

Null string converted to DateTime?;

The overall result is that you can successfully convert an empty string to a datetime type, but if you pass in an illegal date format, the string will get an error and you need to handle it separately.

The Nullableconverter class primarily provides an automatic conversion between nullable types and their underlying primitive types. The specific description of the Nullableconverter class, can be found on the network a large number of information, here is no longer described. Here is the description of MSDN: Http://msdn.microsoft.com/zh-cn/library/system.componentmodel.nullableconverter (v=vs.85). aspx

C # string converted to datetime? (String converted to nullable date type)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.