学海无涯

导航

C#经验

字符串转日期:

/// <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();
  }

列表转换成字典:

   private void AddSpecialCalendarDic(List<SpecialCalendar> specialCalendars)
        {
            if (specialCalendars.Any())
            {
                m_SpecialCalendarDic = specialCalendars.GroupBy(m => m.WorkCenterId).Select(m => new { m.Key, List = m.ToList() }).ToDictionary(k => k.Key, v => v.List);
            }
            else
            {
                m_SpecialCalendarDic.Clear();
            }
        }

  

  

posted on 2023-06-11 22:12  宁静致远.  阅读(17)  评论(0编辑  收藏  举报