C#中String类型和DateTime类型之间的互相转换。

DateTime按照一定格式转化成String:

string format = "yyyyMMdd";
Console.WriteLine(DateTime.Now.ToString(format));

String按照一定格式转化成DateTime:

using System.Globalization;

DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
dtFormat.ShortDatePattern = "yyyy/MM/dd";
DateTime dt = Convert.ToDateTime("2011/05/26", dtFormat);
Console.WriteLine(dt.ToShortDateString());

评论

暂无评论