Solved

Converting and Comparing Dates

Posted on 1998-07-13
5
170 Views
Last Modified: 2010-05-03
Why do I get the same value of 36597 when I type the following in the Immediate Window:
debug.print cdbl(datevalue, "March 12, 2000"))
debug.print cdbl(datevalue, "March 12, 00"))
debug.print cdbl(datevalue, "March 12, 0000"))

I also get the same value of 36926 when I key in the following:
debug.print cdbl(datevalue, "March 12, 2001"))
debug.print cdbl(datevalue, "March 12, 01"))
debug.print cdbl(datevalue, "March 12, 0001"))

I am trying to find a reliable way to compare dates.

                   Sheila
0
Comment
Question by:baileys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:trkcorp
ID: 1465630
Honest, I'm not trying to be a smart alec but whay are you converting it to a double?  Why not convert it to a date variant? I have always found this method reliable...
0
 
LVL 6

Expert Comment

by:alamo
ID: 1465631
trkcorp, that's not a very helpful answer you gave baileys. It ignores the real problem - I think date variants will have the exact same problem. If this isn't the case, then post an example using date variants which works on the above strings.

The real problem is in the datevalue function which converts a string to an internal date. It is documented (in the VB help) as not working on dates before 100AD, which is why March 12, 0001 won't work.

Ignoring years before 100 AD allows VB to assume a year < 100 is just a 2 digit year, and so it automatically adds the century. But then it has to decide which century. For years < 30 it assumes the 21st century, years 30 or more it assumes 1900s (the cutoff point is January 1 30). This is a popular way to "quick fix" Year 2000 problems: instead of increasing the number of digits for the year instead slide the 100-year "window" forward so that a 2 digit year can still work past 1999. This is a lousy way to do it, but sometimes it's that or nothing.

I am assuming < year 1600 or > year 9999 isn't a concern. If you need dates older than about 1600, you will have problems, you'll need Julian dates and those will work back until several thousand BC.

So, if you make sure you use 4 digit years comparing dates the way you are will work just fine (and is the same I believe as using date variants).  Hope this helps!
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 1465632
alamo, you are correct about date span validity and baileys needs to know those limitations.  I was a little brash.  Excusez mois.
0
 

Author Comment

by:baileys
ID: 1465633
I like alamo explanation.
0
 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
ID: 1465634
Thanks baileys, glad I could help explain it!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

733 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