public static string ConvertDynamicAyDateExpression(string expression) { StringBuilder result = new StringBuilder(); DateTime dtnow = DateTime.Now; int uIndex = 0; int axlength = expression.Length; List <string> extString = new List <string>(); string resultString = null; for (int i = 0; i < axlength; i++) { if (expression[i] == '-' || expression[i] == '.' || expression[i] == ' ' || expression[i] == ':') { result.Append(expression[i]); } else if (expression[i] == '%') { string _d = expression.Substring(i, 2); switch (_d) { case "%y": result.Append(dtnow.Year.ToString().PadLeft(2, '0')); i++; break; case "%M": result.Append(dtnow.Month.ToString().PadLeft(2, '0')); i++; break; case "%d": result.Append(dtnow.Day.ToString().PadLeft(2, '0')); i++; break; case "%H": result.Append(dtnow.Hour.ToString().PadLeft(2, '0')); i++; break; case "%m": result.Append(dtnow.Minute.ToString().PadLeft(2, '0')); i++; break; case "%s": result.Append(dtnow.Second.ToString().PadLeft(2, '0')); i++; break; } } else if (expression[i] == '{') { MatchCollection matches = Regex.Matches(expression, @"{(?<c>.*?)}"); string xy = null; foreach (Match match in matches) { GroupCollection groups = match.Groups; xy = groups["c"].Value; break; } if (!xy.IsNullAndTrimAndEmpty()) { extString.Add(xy); i = i + xy.Length + 1; result.Append(zhanwei[uIndex]); uIndex++; } } else { result.Append(expression[i]); } } resultString = result.ToString(); //分析运算式 if (extString.Count > 0) { int extStringIndex = 0; int? _yyyy = null; int? _MM = null; DateTime?dtReturn = null; //临时使用计算add的 foreach (var item in extString) { if (item.IndexOf("%y") == 0) { //如果处理年 int operNo = item.Substring(2).ToInt(); int _ = dtnow.Year + operNo; if (_ > YearStrick.MAXYEAR) { _ = YearStrick.MAXYEAR; } else if (_ < YearStrick.MINYEAR) { _ = YearStrick.MINYEAR; } _yyyy = _; resultString = resultString.Replace(zhanwei[extStringIndex], _yyyy.ToString()); extStringIndex++; } else if (item.IndexOf("%M") == 0) { //如果处理月 int operNo = item.Substring(2).ToInt(); int _ = dtnow.Month + operNo; if (_ > 12) { int _yeI = 0; string _ye = null; if (_yyyy == null) { //从result读取前 _ye = resultString.Substring(0, 4); _yeI = _ye.ToInt(); } else { _ye = _yyyy.Value.ToString(); _yeI = _yyyy.Value; } _yeI++; if (_yeI > YearStrick.MAXYEAR) { _yeI = YearStrick.MAXYEAR; } resultString = resultString.Replace(_ye, _yeI.ToString()); int month = _ - 12; _MM = month; resultString = resultString.Replace(zhanwei[extStringIndex], _MM.ToString()); extStringIndex++; } else if (_ < 1) { int _yeI = 0; string _ye = null; if (_yyyy == null) { //从result读取前 _ye = resultString.Substring(0, 4); _yeI = _ye.ToInt(); } else { _ye = _yyyy.Value.ToString(); _yeI = _yyyy.Value; } _yeI--; if (_yeI < YearStrick.MINYEAR) { _yeI = YearStrick.MINYEAR; } resultString = resultString.Replace(_ye, _yeI.ToString()); int month = _ + 12; _MM = month; resultString = resultString.Replace(zhanwei[extStringIndex], _MM.ToString()); extStringIndex++; } } else if (item.IndexOf("%d") == 0) { //获得年月 int _yeI = 0; string _ye = null; if (_yyyy == null) { //从result读取前 _ye = resultString.Substring(0, 4); _yeI = _ye.ToInt(); } else { _ye = _yyyy.Value.ToString(); _yeI = _yyyy.Value; } int _MeI = 0; string _Me = null; if (_MM == null) { //从result读取前 _Me = resultString.Substring(5, 2); _MeI = _Me.ToInt(); } else { _Me = _MM.Value.ToString(); _MeI = _MM.Value; } int operNo = item.Substring(2).ToInt(); try { dtReturn = new DateTime(_yeI, _MeI, dtnow.Day); dtReturn = dtReturn.Value.AddDays(operNo); } catch { //有时候因为此时 2月没有30,31,4月没有31日一样,这里时间要额外处理 var _2 = AyDatePickerHelper.NumOfDays(_yeI, _MeI); if (dtnow.Day > _2) { int _21 = dtnow.Day - _2; operNo = operNo + _21; dtReturn = new DateTime(_yeI, _MeI, _2); dtReturn = dtReturn.Value.AddDays(operNo); } } var _343 = dtReturn.Value.Day.ToString().PadLeft(2, '0'); resultString = resultString.Replace(zhanwei[extStringIndex], _343); extStringIndex++; } else if (item.IndexOf("%H") == 0) { dtReturn = InitReturnDate(resultString, _yyyy, _MM, dtReturn); dtReturn = dtReturn.Value.AddHours(dtnow.Hour); //dtReturn = new DateTime(dtReturn.Value.Year, dtReturn.Value.Month, dtReturn.Value.Day, dtnow.Hour, 0, 0); int operNo = item.Substring(2).ToInt(); dtReturn = dtReturn.Value.AddHours(operNo); var _343 = dtReturn.Value.Hour.ToString().PadLeft(2, '0'); resultString = resultString.Replace(zhanwei[extStringIndex], _343); extStringIndex++; } else if (item.IndexOf("%m") == 0) { dtReturn = InitReturnDate(resultString, _yyyy, _MM, dtReturn); string _he = resultString.Substring(11, 2); int _heI = _he.ToInt(); dtReturn = new DateTime(dtReturn.Value.Year, dtReturn.Value.Month, dtReturn.Value.Day, _heI, dtnow.Minute, 0); int operNo = item.Substring(2).ToInt(); dtReturn = dtReturn.Value.AddMinutes(operNo); var _343 = dtReturn.Value.Minute.ToString().PadLeft(2, '0'); resultString = resultString.Replace(zhanwei[extStringIndex], _343); extStringIndex++; } else if (item.IndexOf("%s") == 0) { dtReturn = InitReturnDate(resultString, _yyyy, _MM, dtReturn); string _he = resultString.Substring(11, 2); int _heI = _he.ToInt(); string _me = resultString.Substring(14, 2); //17,2代表获得秒 int _meI = _me.ToInt(); dtReturn = new DateTime(dtReturn.Value.Year, dtReturn.Value.Month, dtReturn.Value.Day, _heI, _meI, dtnow.Second); int operNo = item.Substring(2).ToInt(); dtReturn = dtReturn.Value.AddSeconds(operNo); var _343 = dtReturn.Value.Second.ToString().PadLeft(2, '0'); resultString = resultString.Replace(zhanwei[extStringIndex], _343); extStringIndex++; } } } return(resultString); }
private static void ReturnDateDateTime(string teSepc, List <string> strs, DateTime dtnow, ref int curSpecIndex, ref DateTime dtReturn, string _text) { var _p2 = _text.Split('-'); int _p2Length = _p2.Length; for (int i = 0; i < _p2Length; i++) { var _p2_0 = _p2[i]; //当是特殊标记的时候处理 if (_p2_0 == teSepc) { if (strs.Count > curSpecIndex) { string _guize = strs[curSpecIndex]; string _yanma = null; char oper = '+'; int operIndex = 2; if (_guize.IndexOf(yuemo) > -1) { operIndex = 3; oper = _guize[operIndex]; _yanma = _guize.Substring(0, 3); } else { oper = _guize[operIndex]; _yanma = _guize.Substring(0, 2); } int operNo = _guize.Substring(operIndex).ToInt(); //if (oper == '-') //{ // operNo = operNo * (-1); //} if (_yanma == "%y") { int y = dtnow.Year + operNo; dtReturn = new DateTime(y, 1, 1); } else if (_yanma == "%M") { dtReturn = new DateTime(dtReturn.Year, dtnow.Month, 1); dtReturn = dtReturn.AddMonths(operNo); } else if (_yanma == yuemo) { dtReturn = new DateTime(dtReturn.Year, dtReturn.Month, AyDatePickerHelper.NumOfDays(dtReturn.Year, dtReturn.Month)); } else if (_yanma == "%d") { try { dtReturn = new DateTime(dtReturn.Year, dtReturn.Month, dtnow.Day); dtReturn = dtReturn.AddDays(operNo); } catch { //有时候因为此时 2月没有30,31,4月没有31日一样,这里时间要额外处理 var _2 = AyDatePickerHelper.NumOfDays(dtReturn.Year, dtReturn.Month); if (dtnow.Day > _2) { int _21 = dtnow.Day - _2; operNo = operNo + _21; dtReturn = new DateTime(dtReturn.Year, dtReturn.Month, _2); dtReturn = dtReturn.AddDays(operNo); } } } curSpecIndex++; } } else if (_p2_0 == "%y") { dtReturn = new DateTime(dtnow.Year, 1, 1); } else if (_p2_0 == "%M") { dtReturn = new DateTime(dtReturn.Year, dtnow.Month, 1); } else if (_p2_0 == yuemo) { dtReturn = new DateTime(dtReturn.Year, dtReturn.Month, AyDatePickerHelper.NumOfDays(dtReturn.Year, dtReturn.Month)); } else if (_p2_0 == "%d") { try { dtReturn = new DateTime(dtReturn.Year, dtReturn.Month, dtnow.Day); } catch { //有时候因为此时 2月没有30,31,4月没有31日一样,这里时间要额外处理 var _2 = AyDatePickerHelper.NumOfDays(dtReturn.Year, dtReturn.Month); if (dtnow.Day > _2) { int _21 = dtnow.Day - _2; dtReturn = new DateTime(dtReturn.Year, dtReturn.Month, _2); dtReturn = dtReturn.AddDays(_21); } } } else { if (i == 0) { dtReturn = new DateTime(_p2_0.ToInt(), 1, 1); } else if (i == 1) { dtReturn = new DateTime(dtReturn.Year, _p2_0.ToInt(), 1); } else if (i == 2) { try { dtReturn = new DateTime(dtReturn.Year, dtReturn.Month, _p2_0.ToInt()); } catch { //有时候因为此时 2月没有30,31,4月没有31日一样,这里时间要额外处理 int _12 = _p2_0.ToInt(); var _2 = AyDatePickerHelper.NumOfDays(dtReturn.Year, dtReturn.Month); if (_12 > _2) { int _21 = _p2_0.ToInt() - _2; dtReturn = new DateTime(dtReturn.Year, dtReturn.Month, _2); dtReturn = dtReturn.AddDays(_21); } } } } } }