繁体   English   中英

比较vb.net中的两个日期是否相等

[英]Compare two dates in vb.net whether they are equal or not

我有一个Date变量startdt和一个String变量hdnsdate

假设startdt的值格式为3/1/ 2012hdnsdate的值格式为03/01/2012 然后我如何比较vb.net中这两个日期相等。

if我的程序状况良好,我想进行此检查。 如果两个日期匹配, if移出if循环,否则进入if循环。

例如,C#中的示例正是我想要的。

if(startdt !=hdnsdate)
{
 //Do
}
else
{
//Do this
}

使用Parse,ParseExact方法将hdnsdate (字符串)解析为Date类型,并使用DateTime.CompareEqualsCompareTo方法。

至今字符串

 Dim enddate as Date
 Date.TryParse(hdnsdate, enddate)

 If startdt = enddate Then
   'Do this
 Else
   'Do this
 End If

比较日期的替代方法:

Dim result = DateTime.Compare(date1, date2)
If result=0 Then
  'Do this
End If

您需要将字符串解析为DateTime然后将两者进行比较。

VB.NET

Dim parsed As DateTime = DateTime.Parse(hdnsdate)

If startdt != parsed Then

Else

End If

C#:

DateTime parsed = DateTime.Parse(hdnsdate);

if(startdt != parsed )
{
 //Do
}
else
{
//Do this
}

我建议您查看DateTime定义的不同解析方法,因为您可能需要使用标准自定义日期和时间格式的字符串以确保正确解析该字符串。

Dim result = DateTime.Compare(hdnsdate, date2)
If result=0 Then
  'Do this
End If

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM