Solved

Converting and Comparing Dates

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now