473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem when comparing two DateTime variables

I need to compare two DateTime variables named as t1 and t2 using VB.NET
2003. If t1 is any earlier time as t2, only then program should do
something like...

If (t1 < t2) Then
'DoSomethingHere...
End If

But I don't figue out how comparing DateTimes is working.

I tried to compare these variables with same time in Immediate-window...

?t1
#3/30/2005 10:18:14 AM#
?t2
#3/30/2005 10:18:14 AM#
?DateTime.Compare(t1, t2)
1
?DateTime.Compare(t2, t1)
-1
?t1.CompareTo(t2)
1
?t1 = t2
False
?t1.Equals(t2)
False

....so why these t1 ja t2 variables content is not same in this case
according comparing results !?!

--
Thanks in advance!

Mika
Nov 21 '05 #1
4 7319
Mika,

I find the most easy one to compare the ticks

if t1.Ticks < t2.Ticks then

(the bracket is not needed in VBNet)

I hope this helps,

Cor
Nov 21 '05 #2
>
I find the most easy one to compare the ticks

if t1.Ticks < t2.Ticks then
Thanks Cor! This is what I needed.
(the bracket is not needed in VBNet)

I know, but sometimes I use C#, and in my mind brackets make it more
readable - so is there any reasons as an example why I should't use
brackets with VB.NET where brackets are not needed?
Nov 21 '05 #3
Mika,

I know, but sometimes I use C#, and in my mind brackets make it more
readable - so is there any reasons as an example why I should't use
brackets with VB.NET where brackets are not needed?


I find it just not looking good. For me it tells that there is something
extra with the evaluated expression. While that is not in your case.
However, probably just a matter of preference. Therefore do it as you wish.

Cor
Nov 21 '05 #4
On Thu, 12 May 2005 09:28:53 +0300, Mika M
<mahmik_nospam@removethis_luukku.com> wrote:
I need to compare two DateTime variables named as t1 and t2 using VB.NET
2003. If t1 is any earlier time as t2, only then program should do
something like...

If (t1 < t2) Then
'DoSomethingHere...
End If

But I don't figue out how comparing DateTimes is working.

I tried to compare these variables with same time in Immediate-window...


Are you certain they were the same (milliseconds and ticks, not just the
formatted string)?
I'm using > and < operators directly on dates all the time.

Try this code:

Dim d1, d2 As Date
Dim dt1, dt2 As DateTime
d1 = Now
d2 = d1
dt1 = d1
dt2 = d1
Debug.WriteLine((d1 < d2) & ", " & (d1 = d2) & ", " & (d1 > d2))
Debug.WriteLine((dt1 < dt2) & ", " & (dt1 = dt2) & ", " & (dt1 > dt2))

d2 = d2.AddMilliseconds(1)
dt2 = d2
Debug.WriteLine((d1 < d2) & ", " & (d1 = d2) & ", " & (d1 > d2))
Debug.WriteLine((dt1 < dt2) & ", " & (dt1 = dt2) & ", " & (dt1 > dt2))

Result:

False, True, False
False, True, False
True, False, False
True, False, False

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Donnal Walter | last post by:
I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15) >>> print x == y True How can these two...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
10
by: scorpion53061 | last post by:
What I thought would be pretty easy has turned out not to be. I have three variables. Actiontime is formatted as 08/11/2004 11:03PM Actiontime JobStreamStart (11:00:00PM) JobStreamEnd...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
13
by: Jim Armstrong | last post by:
Hi all - This problem has been driving me crazy, and I'm hoping the answer is something stupid I am neglecting to see.... The procedure posted below is part of an Access/SQL database I have...
8
by: sherifffruitfly | last post by:
Hi all, The following function is the ValueChanged handler for two DateTimePicker thingies - allowing the user to specify a date range. I don't understand why the following bug exists: when the...
2
by: Bob | last post by:
Hi, I have a wrong result when testing today against a date fetched from a table in excel. The value in excel is: 14/12/2006 (european format). When today is after that value in excel, the...
8
by: kyosohma | last post by:
Hi, I am writing a reminder program for our Zimbra email client. One of the requirements I was given was to automatically increment or decrement the display to show something like the following:...
6
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am trying to compare just the time portion of two variables that are of date time part. For example I have dtdate1= 2/2/07 10:00:00 dtdate2=3/4/07 9:00:00 so for the comparision I would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.