Solved

Converting and Comparing Dates

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

776 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