字符串转日期:
/// <summary> /// 根据字符串,生成实例 /// </summary> /// <param name="marks"></param> public void GenerateModel(DepartmentCategories category, string marks) { this.DepartmentCategories = category; 姓名 = marks.Trim().Split(ConstHelper.MAO_HAO).First(); marks = marks.Replace(姓名 + ConstHelper.MAO_HAO, "").Trim(); string regexPattern = $@"\b\d{{4}}/\d{{1,2}}/\d{{1,2}} \d{{1,2}}:\d{{1,2}}:\d{{1,2}}\b"; var regResult = Regex.Match(marks, regexPattern); string dateString = string.Empty; if (regResult.Success) { dateString = regResult.Groups.Values.First().ToString().Trim(); marks = marks.Replace(dateString, string.Empty); string format = "yyyy/M/d HH:mm:ss"; DateTime dateTime; if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) { 操作日期 = dateTime; } else { // 转换失败 Console.WriteLine($"无法将{dateString}日期字符串转换为有效的日期。"); } } 原因 = marks.Trim(); }
标签:Trim,经验,string,C#,dateString,dateTime,regResult,marks From: https://www.cnblogs.com/friend/p/17473741.html