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

发布于 2024-12-23 01:59:58 字数 475 浏览 3 评论 0 原文

我有一个日期变量startdt和一个字符串变量hdnsdate

假设 startdt 具有 3/1/2012 形式的值,并且 hdnsdate 具有 03/01/ 形式的值2012 年。我如何比较这两个日期在 vb.net 中是否相等。

在我的程序的 if 条件下,我想进行此检查。如果两个日期匹配,则移出 if 循环,否则进入 if 循环。

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

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

I have a Date variable startdt and a String variable hdnsdate.

Suppose if startdt has the value in the form 3/1/2012 and hdnsdate has the value in the form 03/01/2012. Than how can i compare that these two dates are equal in vb.net.

In if condition of my program i want to do this check. In case the two dates match move out of if loop otherwise go into the if loop.

E.g A sample in C# what exactly i want.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绻影浮沉 2024-12-30 01:59:58

使用 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

Parse hdnsdate (string) to Date type using Parse, ParseExact method and use DateTime.Compare, Equals, CompareTo methods.

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
找回味觉 2024-12-30 01:59:58

您需要将字符串解析为 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 上定义的不同解析方法,因为您可能需要使用 标准自定义日期和时间格式字符串,以确保正确解析字符串。

You need to parse the string into a DateTime then compare the two.

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
}

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.

孤独岁月 2024-12-30 01:59:58
Dim result = DateTime.Compare(hdnsdate, date2)
If result=0 Then
  'Do this
End If
Dim result = DateTime.Compare(hdnsdate, date2)
If result=0 Then
  'Do this
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文