簡體   English   中英

從字符串中提取最新的日期時間

[英]Extract the latest date time from a string

我有以下字符串格式的列值,例如:

第 1 行

PLX[12]@03/21 14:34 (475573-B) FCX[36]@03/22 17:03 (272497-B)LDX[44]@03/22 17:01 (272497-N)

第 2 行

PLX[12]@03/21 14:34 (475573-B)LDX[44]@03/22 17:01 (272497-N)

第 3 行

PLX[12]@03/21 14:34 (475573-B)

需要獲取每一行的最新日期(row1 的03/22 17:03 )並將其轉換為DateTime

DateTime dt = DateTime.ParseExact("03/21 14:34", "MM/dd HH:mm", CultureInfo.InvariantCulture);

需要您幫助從字符串中提取最新日期。 我不明白如何使用正則RegexRegex工作。 請建議

以這種方式解決

private static DateTime ParseDateTime(string dateTimeStr)
{
      var records = dateTimeStr.Split("\r\n");

      // example PLX[12]@03/21 14:34 (475573-B)            
       return records
          .Select(x => DateTime.ParseExact(x.Substring(8, 11), "MM/dd HH:mm", CultureInfo.InvariantCulture))
          .Max();
 }

感謝@oleksa 的建議

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM