Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
date conversion question
Why does this code result in a False result?
Public Sub TimeTest() Dim dte As Date Dim lng As Long dte = Now lng = CLng(dte) Debug.Print (dte = CDate(lng)) End Sub |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
date conversion question
Because Now includes the fraction which represents the time
-- Kind Regards, Niek Otten Microsoft MVP - Excel "WIlliam Barnes" wrote in message ... Why does this code result in a False result? Public Sub TimeTest() Dim dte As Date Dim lng As Long dte = Now lng = CLng(dte) Debug.Print (dte = CDate(lng)) End Sub |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
date conversion question
A date also has decimal places that represent the time.
By converting it to a LONG, the value after the decimal point disappears. When you test "dte" against "lng" you are comparing, say, 38047.25 against 38047. By converting "lng" back to a date you get 38047, that is to say 29/02/2004 00:00:00 (or midnight). That is to say, by losing the decimal places in the first conversion you have created a different figure, and so when you do the final comparison, you get False. -- Regards Andy Wiggins www.BygSoftware.com Home of "Save and BackUp", "The Excel Auditor" and "Byg Tools for VBA" "WIlliam Barnes" wrote in message ... Why does this code result in a False result? Public Sub TimeTest() Dim dte As Date Dim lng As Long dte = Now lng = CLng(dte) Debug.Print (dte = CDate(lng)) End Sub |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
date conversion question
Somehow I thought that dates were represented as integral values. When I
cast it to a Double it works as expected. Thanks. "WIlliam Barnes" wrote in message ... Why does this code result in a False result? Public Sub TimeTest() Dim dte As Date Dim lng As Long dte = Now lng = CLng(dte) Debug.Print (dte = CDate(lng)) End Sub |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
date conversion question
William,
The reason is because Now returns the date and time, so you get something like 29/02/2004 18:45:32, whereas CDate(lng) returns a pure date, like 29/02/2004. They are not the same, so you get False. -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "WIlliam Barnes" wrote in message ... Why does this code result in a False result? Public Sub TimeTest() Dim dte As Date Dim lng As Long dte = Now lng = CLng(dte) Debug.Print (dte = CDate(lng)) End Sub |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Date Conversion | Excel Discussion (Misc queries) | |||
Date Conversion | Excel Discussion (Misc queries) | |||
Date conversion | Excel Discussion (Misc queries) | |||
Another Time Conversion question | Excel Discussion (Misc queries) | |||
Time Conversion question | Excel Discussion (Misc queries) |