public static DateTime?ConvertDV(string expression) { DateTime?dtReturn = null; string str1 = null; string str2 = null; if (expression.IndexOf(",{") > 0) { MatchCollection matches = Regex.Matches(expression, @"#DV\((?<c>.*?),{(?<h>.*?)}"); foreach (Match match in matches) { GroupCollection groups = match.Groups; string _1 = groups["c"].Value; if (_1 != null) { str1 = groups["c"].Value; } string _2 = groups["h"].Value; if (_2 != null) { str2 = groups["h"].Value; } } } else { MatchCollection matches = Regex.Matches(expression, @"#DV\((?<c>.*?)\)"); foreach (Match match in matches) { GroupCollection groups = match.Groups; string _1 = groups["c"].Value; if (_1 != null) { str1 = groups["c"].Value; } } } try { if (str1 == null) { throw new Exception("AY日期表达式中DV片段有错误"); } dtReturn = Convert(str1); if (str2 != null) { AyDCDateExpression _ewai = AyJsonUtility.DecodeObject2 <AyDCDateExpression>(str2); //开始处理 if (_ewai.y != 0) { dtReturn = dtReturn.Value.AddYears(_ewai.y); } if (_ewai.M != 0) { dtReturn = dtReturn.Value.AddMonths(_ewai.M); } if (_ewai.d != 0) { dtReturn = dtReturn.Value.AddDays(_ewai.d); } if (_ewai.H != 0) { dtReturn = dtReturn.Value.AddHours(_ewai.H); } if (_ewai.m != 0) { dtReturn = dtReturn.Value.AddMinutes(_ewai.m); } if (_ewai.s != 0) { dtReturn = dtReturn.Value.AddSeconds(_ewai.s); } } } catch { throw new Exception("AY日期表达式中DV片段中的时间部分不能转换成有效的DateTime"); } return(dtReturn); }
public static DateTime?ConvertD(string dt, string expression, string fmt) { DateTime?dtReturn = null; //读取文本框的值 if (dt.IsNullAndTrimAndEmpty()) { return(dtReturn); } else { MatchCollection matches = Regex.Matches(expression, @"#D\(ay,{(?<c>.*?)}\)"); List <string> strs = new List <string>(); foreach (Match match in matches) { GroupCollection groups = match.Groups; string _1 = groups["c"].Value; if (_1 != null) { strs.Add(groups["c"].Value); } } if (strs.Count > 0) { AyDCDateExpression _ewai = AyJsonUtility.DecodeObject2 <AyDCDateExpression>(strs[0]); try { string[] expectedFormats = { fmt }; dtReturn = DateTime.ParseExact(dt, expectedFormats, AyDatePickerHelper.culture, DateTimeStyles.None); } catch { throw new Exception("AY日期表达式 D函数表达式发生问题"); } //dtReturn = dt.ToDateTime(); //开始处理 if (_ewai.y != 0) { dtReturn = dtReturn.Value.AddYears(_ewai.y); } if (_ewai.M != 0) { dtReturn = dtReturn.Value.AddMonths(_ewai.M); } if (_ewai.d != 0) { dtReturn = dtReturn.Value.AddDays(_ewai.d); } if (_ewai.H != 0) { dtReturn = dtReturn.Value.AddHours(_ewai.H); } if (_ewai.m != 0) { dtReturn = dtReturn.Value.AddMinutes(_ewai.m); } if (_ewai.s != 0) { dtReturn = dtReturn.Value.AddSeconds(_ewai.s); } return(dtReturn); } else { //这里要换成 tryparseextra方式的时间,如果转换失败就当空白处理 dtReturn = dt.ToDateTime(); return(dtReturn); } } }