PC Review


Reply
Thread Tools Rate Thread

Date conversion doesn't make sense

 
 
merlynknight@msn.com
Guest
Posts: n/a
 
      26th Nov 2009
I am having problems with converting text to date using the cdate
function
When I convert .5 into a format of HH:MM AM/PM it becomes 12:00 PM
which makes sense because noon is half of a day

When I convert 0.5 into a format of HH:MM AM/PM it becomes 12:05 AM
which doesn't make sense

When I convert 0.50 into a format of HH:MM AM/PM it becomes 12:50 AM
which also doesn't make sense

When I convert 0.500 into a format of HH:MM AM/PM it becomes 12:00 PM

Below is a msgbox that demonstrates this.
What is happening?

Sub test1()
MsgBox "The string "".5"" converts to " & Format(CDate(".5"), "MMM
dd yyyy HH:MM AM/PM") _
& vbLf & "The string ""0.5"" converts to " & Format(CDate("0.5"),
"MMM dd yyyy HH:MM AM/PM") _
& vbLf & "The string ""0.50"" converts to " &
Format(CDate("0.50"), "MMM dd yyyy HH:MM AM/PM") _
& vbLf & "The string ""0.500"" converts to " &
Format(CDate("0.500"), "MMM dd yyyy HH:MM AM/PM")
End Sub

Thanks for any help
Merlyn
 
Reply With Quote
 
 
 
 
JoeU2004
Guest
Posts: n/a
 
      26th Nov 2009
<(E-Mail Removed)> wrote:
> I am having problems with converting text
> to date using the cdate function
> When I convert .5 into a format of HH:MM AM/PM


But that is not what you are doing. You are converting the result of CDate
into a date or time format.

I think you would get some insight into your mistake if you simply did
MsgBox CDate(...), where "..." is ".5", "0.5", "0.50" and "0.500".

Seeing the presumably unexpected results of CDate, you might be motivated to
read the Help page for CDate (actually Type Conversion Functions).

There, you will see that while the result of CDate is a date/time serial
number, the parameter should be a "date expression", which is defined as:

"Any expression that can be interpreted as a date, including date literals,
numbers that look like dates, strings that look like dates, and dates
returned from functions. A date expression is limited to numbers or strings,
in any combination, that can represent a date from January 1, 100 - December
31, 9999."

IMHO, ".5" et al do not fit the definition of a "date expression". I'm
surprised it does not simply result in an error.

Bottom line: I think you want to do simply Format(.5,"hh:mm AM/PM"),
Format(0.5,"hh:mm AM/PM"), etc. Note that there are no quotes around .5,
0.5 et al. So obviously they will all convert to the same formatted time,
namely 12:00 PM.

If you are starting with the string ".5" etc, you can write
Format(--".5","hh:mm AM/PM"). The "--" (double negative) arithmetic
operation has the effect of converting the string to the equivalent number.


----- original message -----

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I am having problems with converting text to date using the cdate
> function
> When I convert .5 into a format of HH:MM AM/PM it becomes 12:00 PM
> which makes sense because noon is half of a day
>
> When I convert 0.5 into a format of HH:MM AM/PM it becomes 12:05 AM
> which doesn't make sense
>
> When I convert 0.50 into a format of HH:MM AM/PM it becomes 12:50 AM
> which also doesn't make sense
>
> When I convert 0.500 into a format of HH:MM AM/PM it becomes 12:00 PM
>
> Below is a msgbox that demonstrates this.
> What is happening?
>
> Sub test1()
> MsgBox "The string "".5"" converts to " & Format(CDate(".5"), "MMM
> dd yyyy HH:MM AM/PM") _
> & vbLf & "The string ""0.5"" converts to " & Format(CDate("0.5"),
> "MMM dd yyyy HH:MM AM/PM") _
> & vbLf & "The string ""0.50"" converts to " &
> Format(CDate("0.50"), "MMM dd yyyy HH:MM AM/PM") _
> & vbLf & "The string ""0.500"" converts to " &
> Format(CDate("0.500"), "MMM dd yyyy HH:MM AM/PM")
> End Sub
>
> Thanks for any help
> Merlyn


 
Reply With Quote
 
JoeU2004
Guest
Posts: n/a
 
      26th Nov 2009
Errata....

I wrote:
> I think you would get some insight into your
> mistake if you simply did MsgBox CDate(...)


I meant:

Dim x as Double
x = CDate("..."): MsgBox x

where "..." is ".5", "0.5", "0.50" and "0.500".


----- original message -----

"JoeU2004" <joeu2004> wrote in message
news:(E-Mail Removed)...
> <(E-Mail Removed)> wrote:
>> I am having problems with converting text
>> to date using the cdate function
>> When I convert .5 into a format of HH:MM AM/PM

>
> But that is not what you are doing. You are converting the result of
> CDate into a date or time format.
>
> I think you would get some insight into your mistake if you simply did
> MsgBox CDate(...), where "..." is ".5", "0.5", "0.50" and "0.500".
>
> Seeing the presumably unexpected results of CDate, you might be motivated
> to read the Help page for CDate (actually Type Conversion Functions).
>
> There, you will see that while the result of CDate is a date/time serial
> number, the parameter should be a "date expression", which is defined as:
>
> "Any expression that can be interpreted as a date, including date
> literals, numbers that look like dates, strings that look like dates, and
> dates returned from functions. A date expression is limited to numbers or
> strings, in any combination, that can represent a date from January 1,
> 100 - December 31, 9999."
>
> IMHO, ".5" et al do not fit the definition of a "date expression". I'm
> surprised it does not simply result in an error.
>
> Bottom line: I think you want to do simply Format(.5,"hh:mm AM/PM"),
> Format(0.5,"hh:mm AM/PM"), etc. Note that there are no quotes around .5,
> 0.5 et al. So obviously they will all convert to the same formatted time,
> namely 12:00 PM.
>
> If you are starting with the string ".5" etc, you can write
> Format(--".5","hh:mm AM/PM"). The "--" (double negative) arithmetic
> operation has the effect of converting the string to the equivalent
> number.
>
>
> ----- original message -----
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I am having problems with converting text to date using the cdate
>> function
>> When I convert .5 into a format of HH:MM AM/PM it becomes 12:00 PM
>> which makes sense because noon is half of a day
>>
>> When I convert 0.5 into a format of HH:MM AM/PM it becomes 12:05 AM
>> which doesn't make sense
>>
>> When I convert 0.50 into a format of HH:MM AM/PM it becomes 12:50 AM
>> which also doesn't make sense
>>
>> When I convert 0.500 into a format of HH:MM AM/PM it becomes 12:00 PM
>>
>> Below is a msgbox that demonstrates this.
>> What is happening?
>>
>> Sub test1()
>> MsgBox "The string "".5"" converts to " & Format(CDate(".5"), "MMM
>> dd yyyy HH:MM AM/PM") _
>> & vbLf & "The string ""0.5"" converts to " & Format(CDate("0.5"),
>> "MMM dd yyyy HH:MM AM/PM") _
>> & vbLf & "The string ""0.50"" converts to " &
>> Format(CDate("0.50"), "MMM dd yyyy HH:MM AM/PM") _
>> & vbLf & "The string ""0.500"" converts to " &
>> Format(CDate("0.500"), "MMM dd yyyy HH:MM AM/PM")
>> End Sub
>>
>> Thanks for any help
>> Merlyn

>


 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      26th Nov 2009
Change
CDate(".5")
to
CDate(Val(".5"))

or even don't use CDate at all, only use Val()

Reason is CDate assumes user the string is already 'like' a time, rather
than an ordinary number

Regards,
Peter T


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I am having problems with converting text to date using the cdate
> function
> When I convert .5 into a format of HH:MM AM/PM it becomes 12:00 PM
> which makes sense because noon is half of a day
>
> When I convert 0.5 into a format of HH:MM AM/PM it becomes 12:05 AM
> which doesn't make sense
>
> When I convert 0.50 into a format of HH:MM AM/PM it becomes 12:50 AM
> which also doesn't make sense
>
> When I convert 0.500 into a format of HH:MM AM/PM it becomes 12:00 PM
>
> Below is a msgbox that demonstrates this.
> What is happening?
>
> Sub test1()
> MsgBox "The string "".5"" converts to " & Format(CDate(".5"), "MMM
> dd yyyy HH:MM AM/PM") _
> & vbLf & "The string ""0.5"" converts to " & Format(CDate("0.5"),
> "MMM dd yyyy HH:MM AM/PM") _
> & vbLf & "The string ""0.50"" converts to " &
> Format(CDate("0.50"), "MMM dd yyyy HH:MM AM/PM") _
> & vbLf & "The string ""0.500"" converts to " &
> Format(CDate("0.500"), "MMM dd yyyy HH:MM AM/PM")
> End Sub
>
> Thanks for any help
> Merlyn



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
AbandonedMutexException doesn't make sense. dvestal@gmail.com Microsoft C# .NET 1 6th Aug 2011 12:41 AM
This doesn't make any sense Howard Windows Vista General Discussion 3 24th Mar 2006 02:16 PM
Odd exception that doesn't make sense to me =?Utf-8?B?YmFsbWVyY2g=?= Microsoft Dot NET 1 24th Feb 2006 07:28 AM
a StackOverflowException which doesn't make sense =?Utf-8?B?SmFrdWIgR2FqZGE=?= Microsoft C# .NET 2 31st Mar 2004 03:20 PM
World doesn't make sense (at least my IDE setup doesn't ;-)) Carlos Moreno Storage Devices 3 8th Jul 2003 10:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 AM.