Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Date conversion doesn't make sense

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Date conversion doesn't make sense

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 -----

wrote in message
...
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Date conversion doesn't make sense

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
...
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 -----

wrote in message
...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Date conversion doesn't make sense

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


wrote in message
...
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
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
make signed over-punch conversion work in a macro Alex Excel Programming 0 January 4th 07 05:27 AM
Converting a date from nonsense to sense RobertM Excel Discussion (Misc queries) 1 April 24th 06 02:21 PM
Does this make sense? Tom Excel Programming 0 August 24th 04 06:24 PM


All times are GMT +1. The time now is 02:11 AM.

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

About Us

"It's about Microsoft Excel"