小知识点记录笔记

该博文记录我在开发过程中遇到的细小问题,以备后用。

(1)从txt中解析DateTime类型,格式为20140506或者20140506 12:30:31  

1 System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);

2 string str1 = "20140812";

3 string str2 = "20140812 16:41:30";

4 DateTime time1 = DateTime.ParseExact(str1, "yyyyMMdd", format);

5 DateTime time2 = DateTime.ParseExact(str2, "yyyyMMdd HH:mm:ss", format);

 

(2)从excel中读取日期  

 1 private bool ParseDateTime(string cellValue, out string strDate)

 2 {

 3     DateTime date = default(DateTime);

 4     double value = default(double);

 5     if (double.TryParse(cellValue, out value))

 6     {

 7         strDate = DateTime.FromOADate(value).ToString("yyy-MM-dd HH:mm:ss");

 8     }

 9     else

10     {

11         if (DateTime.TryParse(cellValue, out date))

12         {

13             strDate = date.ToString("yyy-MM-dd HH:mm:ss");

14         }

15         else

16         {

17             strDate = string.Empty;

18         }

19     }

20     string reg = @"^(\d{4})-([0-1]\d)-([0-3]\d)\s([0-2]\d):([0-5]\d):([0-5]\d)$";

21     if (Regex.IsMatch(strDate, reg))

22     {

23         return true;

24     }

25     return false;

26 }

 

你可能感兴趣的:(笔记)