vb.net 如何只比较日期部分而不是两个日期的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/618878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:06:30  来源:igfitidea点击:

How to compare just the date part and not the time of two Dates?

vb.netdatecompare

提问by Ylva D

I want to compare just the date part (and Not the time) of two VB.NET Date objects. Is there a way to do that?

我只想比较两个 VB.NET Date 对象的日期部分(而不是时间)。有没有办法做到这一点?

回答by Jon Skeet

Just take the date part of each via the Dateproperty and compare the two:

只需通过Date属性获取每个的日期部分并比较两者:

date1.Date.CompareTo(date2.Date)

Or:

或者:

If date1.Date < date2.Date Then

回答by Paul Herzberg

You could also use TimeSpan

您还可以使用 TimeSpan

Dim ts As TimeSpan
ts = dt1 - dt2

ts.Days will now have the difference of the two dates as whole days.

ts.Days 现在将两个日期的差异作为整天。

回答by Hans Passant

Compare the DateTime.Date properties.

比较 DateTime.Date 属性。

回答by Wavare Santosh

Change the txt1 date to format dd/mm/yyyy using myDateTime.ToShortDateString()so that both the dates will be in same format. then :

将 txt1 日期更改为 dd/mm/yyyy 格式,myDateTime.ToShortDateString()以便两个日期的格式相同。然后 :

if (DateTime.Compare(date1, date2) > 0) 
// which means ("date1 > date2")
if (DateTime.Compare(date1, date2) == 0) 
//which means ("date1 == date2");
if (DateTime.Compare(date1, date2) < 0) 
//which means ("date1 < date2");

回答by sun sreng

Dim backDateCount As Integer = DateDiff(DateInterval.Day, CDate(dtpCheckIn.SelectedDate.Value).Date, Date.Now.Date)

Date.Now.Date:#12/4/2018 12:00:00 AM#

Date.Now:#12/4/2018 03:23:34 PM#

日期.现在.日期:#12/4/2018 12:00:00 AM#

日期。现在:#12/4/2018 03:23:34 PM#

回答by Talal Khaled Bani Hani

Dim date1, date2 As Date
date1 = Date.Parse(dtpStart.Text)
date2 = Date.Parse(dtpEnd.Text)
If (DateTime.Compare(date1, date2) > 0) Then ' which means ("date1 > date2") 
    MessageBox.Show("??? ?????  ?????? ??????? ???? ????  ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading)
    Exit Sub
End If