473,796 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
'DoSomethingHer e...
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.Compa re(t1, t2)
1
?DateTime.Compa re(t2, t1)
-1
?t1.CompareTo(t 2)
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 7337
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_luuk ku.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
'DoSomethingHer e...
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.AddMilliseco nds(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
13133
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 objects be considered equal? Is there a *general* way to test for date != datetime as well as 4.5 != 4.6?
2
3347
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
1923
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:00:00AM) If (Format(CDate(ActionTime), "t") >= JobStreamStart AND (Format(CDate(ActionTime), "t") < jobstreamend Then
2
4558
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 set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
13
10708
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 developed. Basically, the user would input a beginning and ending date, and the query goes and pulls records that meet the following criteria: 1. TradeDate is between beginning date and ending date
8
1688
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 first picker is set later than the second, the messagebox pops up *twice* in succession. What's causing that? How can I achieve the (obvious) desired effect?
2
1404
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 message "time is over" must appear, otherwise nothing. My problem is that the message appear already when today = that value. Any idea why?
8
1191
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: 5 minutes until appointment or
6
2588
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 want 10:00:00 cmp with 9:00:00 so ddate1>ddate2 is true. I tried converting to short time string but the comparison can not be done on the strings. Thanks Paul. --
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.