Solved

Converting and Comparing Dates

Posted on 1998-07-13
5
172 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

Industry Leaders: 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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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