// parameters: // strOrigin 8 字符的日期时间。本地时间 public static int Date8toRfc1123(string strOrigin, out string strTarget, out string strError) { strError = ""; strTarget = ""; // 格式为 20060625, 需要转换为rfc if (strOrigin.Length != 8) { strError = "源日期字符串 '" + strOrigin + "' 格式不正确,应为8字符"; return(-1); } DateTime time; try { time = DateTimeUtil.Long8ToDateTime(strOrigin); } catch { strError = "日期字符串 '" + strOrigin + "' 字符串转换为DateTime对象时出错"; return(-1); } /* * IFormatProvider culture = new CultureInfo("zh-CN", true); * * DateTime time; * try * { * time = DateTime.ParseExact(strOrigin, "yyyyMMdd", culture); * } * catch * { * strError = "日期字符串 '" + strOrigin + "' 字符串转换为DateTime对象时出错"; * return -1; * } * time = time.ToUniversalTime(); * strTarget = DateTimeUtil.Rfc1123DateTimeString(time); */ strTarget = DateTimeUtil.Rfc1123DateTimeStringEx(time); return(0); }
// 规范化8字符的日期字符串 public static string CanonicalizePublishTimeString(string strText) { if (strText.Length == 4) { strText = strText + "0101"; goto END1; } if (strText.Length == 6) { strText = strText + "01"; goto END1; } if (strText.Length == 8) { goto END1; } if (strText.Length == 10) { goto END1; } throw new Exception("出版日期字符串 '" + strText + "' 格式不正确"); END1: // 检查一下时间字符串是否属于存在的时间 string strTest = strText.Substring(0, 8); try { DateTimeUtil.Long8ToDateTime(strTest); } catch (System.ArgumentOutOfRangeException /*ex*/) { throw new Exception("日期字符串 '" + strText + "' 不正确: 出现了不可能的年、月、日值"); } catch (Exception ex) { throw new Exception("日期字符串 '" + strText + "' 不正确: " + ex.Message); } return(strText); }
// 将日期字符串解析为起止范围日期 // throw: // Exception public static void ParseDateRange(string strText, out string strStartDate, out string strEndDate) { strStartDate = ""; strEndDate = ""; int nRet = strText.IndexOf("-"); if (nRet == -1) { // 没有'-' if (strText.Length == 4) { strStartDate = strText + "0101"; strEndDate = strText + "1231"; return; } if (strText.Length == 6) { strStartDate = strText + "01"; DateTime start = DateTimeUtil.Long8ToDateTime(strStartDate); DateTime end = start.AddMonths(1); end = new DateTime(end.Year, end.Month, 1); // 下月1号 end = end.AddDays(-1); // 上月最后一号 strEndDate = strText + end.Day; return; } if (strText.Length == 8) { // 单日 strStartDate = strText; strEndDate = ""; return; } // text-level: 用户提示 throw new Exception( string.Format("日期字符串 '{0}' 格式不正确。应当为4/6/8字符", strText) ); } else { string strLeft = ""; string strRight = ""; strLeft = strText.Substring(0, nRet).Trim(); strRight = strText.Substring(nRet + 1).Trim(); if (strLeft.Length != strRight.Length) { // text-level: 用户提示 throw new Exception( string.Format("日期字符串 '{0}' 格式不正确。横杠左边的部分 '{1}' 和右边的部分 '{2}' 字符数应相等。", strText, strLeft, strRight) ); } if (strLeft.Length == 4) { strStartDate = strLeft + "0101"; strEndDate = strRight + "1231"; return; } if (strLeft.Length == 6) { strStartDate = strLeft + "01"; DateTime start = DateTimeUtil.Long8ToDateTime(strRight + "01"); DateTime end = start.AddMonths(1); end = new DateTime(end.Year, end.Month, 1); // 下月1号 end = end.AddDays(-1); // 上月最后一号 strEndDate = strRight + end.Day; return; } if (strLeft.Length == 8) { // 单日 strStartDate = strLeft; strEndDate = strRight; return; } // text-level: 用户提示 throw new Exception( string.Format("日期字符串 '{0}' 格式不正确。横杠左边或者右边的部分,应当为4/6/8字符", strText) ); } }