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

可紊 提交于 2019-11-30 05:59:44
Jon Skeet

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

date1.Date.CompareTo(date2.Date)

Or:

If date1.Date < date2.Date Then

Compare the DateTime.Date properties.

You could also use TimeSpan

Dim ts As TimeSpan
ts = dt1 - dt2

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

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 :

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");
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#

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!