Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DateTime.Equal returns false when true?

Posted on 2013-05-31
4
Medium Priority
?
357 Views
Last Modified: 2013-06-03
Code below is returning FALSE when I'd expect TRUE as comparing the SAME Dates.

Please advise

CODE
Debug.Print(Timestamp.ToString)
Debug.Print(Timestamp2.ToString)
Dim dateEqual = DateTime.Equals(Timestamp, Timestamp2)
Debug.Print(dateEqual)

OUTPUT from console
31/05/2013 16:56:19
31/05/2013 16:56:19
False
0
Comment
Question by:rwallacej
  • 2
4 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 39211226
maybe its including milliseconds?
0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 400 total points
ID: 39211247
seems that method relies on the Ticks property of the DateTime object.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1600 total points
ID: 39211319
If you want to know if the dates are equal, ignoring the time portion, then:
        Dim dateEqual = DateTime.Equals(Timestamp.Date, Timestamp2.Date)

Open in new window


Or, if you want to truncate the milliseconds portions, then compare:
*From Joe over at stackoverflow.
        Dim Timestamp As DateTime = DateTime.Now
        Dim Timestamp2 As DateTime = DateTime.Now

        Timestamp = Timestamp.AddTicks(-(Timestamp.Ticks Mod TimeSpan.TicksPerSecond))
        Timestamp2 = Timestamp2.AddTicks(-(Timestamp2.Ticks Mod TimeSpan.TicksPerSecond))

        Debug.Print(Timestamp.ToString)
        Debug.Print(Timestamp2.ToString)
        Dim dateEqual = DateTime.Equals(Timestamp.Date, Timestamp2.Date)
        Debug.Print(dateEqual)

Open in new window

0
 

Author Closing Comment

by:rwallacej
ID: 39215882
thanks for help
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question