比较 vb.net 中的字符串日期

Compare string dates in vb.net

您好,在我的程序中,为了我的应用程序,我正在转换字符串中的日期。我想比较这些字符串,看看日期之间是否有适当的差异。
示例 date1 = "07/02/2015 12:12:000"date2 = "08/02/2015 16:15:000".
我如何才能只比较年份值而不解析字符串的日期。提前致谢。 我已经尝试过 .Length - 一些但没有运气。

转换为 DateTime 个对象,然后使用正常的算术运算来比较它们。

dim d1 = DateTime.Parse(input1); // Use ParseExact or more control
dim d2 = DateTime.Parse(input2);
If d1.Year != d2.Year Then
  ' years do not match
End If

' or even...
If (d1 - d2).Days > 365 Then
  // more than a year apart (modulo leap years)
End If

Meta 评论: 我知道你说过 "without parsing",但除非你有充分的理由(并告诉我们)并且明智的答案是解析:因为做对越来越容易。