[英]Compare two dates in vb.net whether they are equal or not
I have a Date variable startdt
and a String variable hdnsdate
. 我有一个Date变量startdt
和一个String变量hdnsdate
。
Suppose if startdt
has the value in the form 3/1/2012 and hdnsdate
has the value in the form 03/01/2012 . 假设startdt
的值格式为3/1/ 2012 , hdnsdate
的值格式为03/01/2012 。 Than how can i compare that these two dates are equal in vb.net . 然后我如何比较vb.net中这两个日期相等。
In if
condition of my program i want to do this check. if
我的程序状况良好,我想进行此检查。 In case the two dates match move out of if
loop otherwise go into the if
loop. 如果两个日期匹配, if
移出if
循环,否则进入if
循环。
Eg A sample in C# what exactly i want. 例如,C#中的示例正是我想要的。
if(startdt !=hdnsdate)
{
//Do
}
else
{
//Do this
}
Parse hdnsdate
(string) to Date type using Parse, ParseExact method and use DateTime.Compare , Equals
, CompareTo
methods. 使用Parse,ParseExact方法将hdnsdate
(字符串)解析为Date类型,并使用DateTime.Compare , Equals
和CompareTo
方法。
String to date 至今字符串
Dim enddate as Date
Date.TryParse(hdnsdate, enddate)
If startdt = enddate Then
'Do this
Else
'Do this
End If
Alternative to compare date: 比较日期的替代方法:
Dim result = DateTime.Compare(date1, date2)
If result=0 Then
'Do this
End If
You need to parse the string into a DateTime
then compare the two. 您需要将字符串解析为DateTime
然后将两者进行比较。
VB.NET VB.NET
Dim parsed As DateTime = DateTime.Parse(hdnsdate)
If startdt != parsed Then
Else
End If
C#: C#:
DateTime parsed = DateTime.Parse(hdnsdate);
if(startdt != parsed )
{
//Do
}
else
{
//Do this
}
I suggest looking at the different parsing methods defined on DateTime
, as you may need to use a standard or custom date and time format string to ensure the string gets parsed correctly. 我建议您查看DateTime
定义的不同解析方法,因为您可能需要使用标准或自定义日期和时间格式的字符串以确保正确解析该字符串。
Dim result = DateTime.Compare(hdnsdate, date2)
If result=0 Then
'Do this
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.