Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Conversion Terry Excel Discussion (Misc queries) 1 March 18th 10 07:27 PM
Date Conversion Lindsey Excel Discussion (Misc queries) 4 March 18th 09 04:21 PM
Date conversion Tom Excel Discussion (Misc queries) 7 January 2nd 07 09:47 PM
Another Time Conversion question Sean Skallerud Excel Discussion (Misc queries) 1 August 24th 05 05:17 PM
Time Conversion question Sean Skallerud Excel Discussion (Misc queries) 4 July 21st 05 08:53 PM


All times are GMT +1. The time now is 05:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"