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

时间:2015-02-19 13:23:25

标签: vb.net string date datetime

嗨,在我的程序中,我正在将字符串中的日期转换为我的应用程序。我想比较这些字符串,看看日期之间是否有适当的差异。
示例date1 = "07/02/2015 12:12:000"date2 = "08/02/2015 16:15:000"
如何在不解析字符串的情况下仅比较年份值的初学者。提前致谢。 我尝试了.Length - 但是没有运气。

1 个答案:

答案 0 :(得分:4)

转换为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

元评论:我知道你说“没有解析”,但除非你有一个非常好的理由(并告诉我们)并且明智的答案将是解析:因为它更容易和远更容易做对。

相关问题