// 根据当前日期获取 格式20160101类型的日期 public static string DateString() { DateTime date = DateTime.Now; string tmp = "" + date.Year; tmp += (date.Month < 10 ? "0" : "") + date.Month; tmp += (date.Day < 10 ? "0" : "") + date.Day; return tmp; } // DateTime.Compare(time1, time2); // 从字符串获取日期 //string mytime = "2005-6-7 12:23:34"; public static DateTime parseDate(string date) { IFormatProvider culture = new System.Globalization.CultureInfo("zh-CN", true); string[] expectedFormats = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-d HH:mm:ss", "yyyy-M-dd HH:mm:ss", "yyyy-M-dd HH:mm:ss" }; DateTime dateTime = DateTime.ParseExact(date, expectedFormats, culture, System.Globalization.DateTimeStyles.AllowInnerWhite); return dateTime; } // 对两个字符串日期进行比较 public static int cmp(string str1, string str2) { //DateTime date1 = Tool.parseDate("2005-6-07 12:23:34"); //DateTime date2 = Tool.parseDate("2005-07-07 12:23:34"); //return -1 DateTime date1 = Tool.parseDate(str1); DateTime date2 = Tool.parseDate(str2); return DateTime.Compare(date1, date2); }