Solved

Converting and Comparing Dates

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now