ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   date conversion question (https://www.excelbanter.com/excel-programming/292976-date-conversion-question.html)

William Barnes

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



Niek Otten

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





Andy Wiggins[_3_]

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





William Barnes

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





Bob Phillips[_6_]

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






All times are GMT +1. The time now is 08:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com