C# 转换字符串日期并与其他日期时间比较

C# Convert string date and compare with other date times

提问人:phibac 提问时间:10/27/2023 最后编辑:yaccphibac 更新时间:10/27/2023 访问量:50

问:

我有三个列表框,里面装满了项目。例如,每个项目字符串都以“Oct 26 12:07:33”... 开头。当我单击其他两个列表框中的三个列表框之一中的一个项目时,应自动选择具有相同/最接近时差的项目。

我从列表框项目中读取日期时间并拆分字符串。

string line = listBox2.SelectedItem as string;
string[] lineSplit = line.Split(" ");

string month = (lineSplit[0]);
string day = lineSplit[1];
string time = lineSplit[2];

然后我会创建一个列表并用时差填充它。差异最接近的行索引是我的列表框项的索引。

在其他列表框中查找:

List<int> minDiff = new List<int>();

for (int k = 0; k < listBox3.Items.Count; k++)
{
    string listBox3Line = listBox3.Items[k].ToString();
    string[] listBox3lineSplit = listBox3Line.Split(" ");

    string listBox3month = listBox3lineSplit[0];
    string listBox3day = listBox3lineSplit[1];
    string listBox3time = listBox3lineSplit[2];

    -----Compare------ 



}
  1. 我无法将此字符串转换为日期时间“字符串'Oct/26/12:07:33'未被识别为有效的DateTime。

  2. 有没有另一种简单的方法来比较字符串并找到最接近的差异?

string month = "Oct";
string day = "26";
string time = "12:07:33";

DateTime enter_date = Convert.ToDateTime("Oct" + "/" + "26" + "/" + "12:07:33");
Console.WriteLine(enter_date);

----> "String 'Oct/26/12:07:33' was not recognized as a valid DateTime."

(来自评论)

带有日期和时间戳的 .txt 文件(日志)将在设备上生成。我不可能也无权改变这一点。我可以将它们复制到我的本地机器并读取它们。

C# 日期时间 比较

评论

0赞 Ralf 10/27/2023
尽量不要将 DateTime 作为字符串作为相关信息。始终将其作为 DateTime,并且仅将其转换为字符串(以当前用户当时适合的文化方式)以在某处显示它。例如,在 UI 中。因此,您的实际问题从反问题开始。为什么它首先是字符串?
0赞 phibac 10/27/2023
带有日期和时间戳的 .txt 文件(日志)将在设备上生成。我不可能也无权改变这一点。我可以将它们复制到我的本地机器并读取它们。你的意思是比较问题?
0赞 Ralf 10/27/2023
这对我来说意味着,在读取文件时,您需要找出该字符串的格式。就像符合任何文化中任何标准的缩写月份名称一样。如果是,则使用具有已知格式的 DateTime.ParseExact 作为模板来获取正确的 DateTime,并在后续中使用该时间。比如在列表框的某个地方显示它,比较它们等。

答:

0赞 phibac 10/27/2023 #1

这可能是一个解决方案

string month = "Jan";
string month = "Feb";
.
.
.
.
string month = "Oct";

if (month == "Oct")
{
    int monthInt = 10;
    Console.WriteLine(monthInt);
}

string day = "26";
int dayInt = Int32.Parse(day);
Console.WriteLine(dayInt);

//string time = "12:07:33";

string hour = "12";
int hourInt = Int32.Parse(hour);
Console.WriteLine(hourInt);

string minute = "07";
int minuteInt = Int32.Parse(minute);
Console.WriteLine(minuteInt);

string seconds = "33";
int secondsInt = Int32.Parse(seconds);
Console.WriteLine(secondsInt);

转换每个字符串对,如月、日、小时、分钟、秒,并将这些值与其他值进行比较。为此,我必须用“:”对时间戳再做一个字符串拆分。

0赞 glboothby 10/27/2023 #2

如果您的文本是“Oct/26/12:07:33”,并假设它是月/日/小时:分钟/秒,那么您可以通过以下方式将其转换为日期时间:

string text = "Oct/26/12:07:33";
DateTime date;
if (!DateTime.TryParseExact(text, "MMM/dd/HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) throw new System.FormatException($"Cannot parse {text} to date.");
// Do something with date.

但是日期字符串缺少年份,因此要小心比较。

编辑

刚刚注意到您的问题的开头的字符串为“Oct 26 12:07:33”,在这种情况下

string text = "Oct 26 12:07:33";
DateTime date;
if (!DateTime.TryParseExact(text, "MMM dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) throw new System.FormatException($"Cannot parse {text} to date.");
// Do something with date.

同样,要小心比较,因为你仍然错过了这一年。