## [Solved] Convert with variable

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

redfield
Posts: 66
Joined: Thu Apr 04, 2019 1:41 pm

### [Solved] Convert with variable

Hi All,
I would like to know why the following code results in expected "4:15 PM":

Code: Select all

convert "09:15 AM" to dateItems
add 7 to item 4 of it
convert it to time
Whereas this code:

Code: Select all

put "09:15 AM" into varTime
convert varTime to dateItems
add 7 to item 4 of it
convert it to time
results in ",,,7". So it seems that I can't throw the time string "09:15 AM" into a variable and then convert it?
Last edited by redfield on Sun Jun 16, 2019 12:48 pm, edited 1 time in total.

Klaus
Posts: 11872
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

### Re: Convert with variable

Hi Redfield,

in your first example you do not use a variable, so LC puts everything into IT.
The second example actually uses a variable, so you need to do this, too, of course!
This works:

Code: Select all

...
put "09:15 AM" into varTime
convert varTime to dateItems
add 7 to item 4 of varTime
convert varTime to time
put varTime
## -> 4:15 PM
...
Best

Klaus

SparkOut
Posts: 2260
Joined: Sun Sep 23, 2007 4:58 pm

### Re: Convert with variable

Yes you can. But step through your code in the debugger and see the value of the it variable.
When you convert the variable varTime, there is no "it" variable - the converted value is in the varTime variable already.

You can either

Code: Select all

put "09:15 AM" into varTime
convert varTime to dateItems
add 7 to item 4 of varTime
convert varTime to time
put varTime
or

Code: Select all

put "09:15 AM" into varTime
convert varTime to dateItems
get varTime
add 7 to item 4 of it
convert it to time
put it

SparkOut
Posts: 2260
Joined: Sun Sep 23, 2007 4:58 pm

### Re: Convert with variable

Why do I always get outklaussed?

Klaus
Posts: 11872
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

### Re: Convert with variable

SparkOut wrote:
Sat Jun 08, 2019 10:47 am
Why do I always get outklaussed?
This is a rhetorical question, right?

SparkOut
Posts: 2260
Joined: Sun Sep 23, 2007 4:58 pm

### Re: Convert with variable

Of course! You're obviously much klaussier than me!

bogs
Posts: 4777
Joined: Sat Feb 25, 2017 10:45 pm

### Re: Convert with variable

SparkOut wrote:
Sat Jun 08, 2019 10:47 am
Why do I always get outklaussed?
Huh, I didn't even make it to the gate~!

sritcp
Posts: 426
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

### Re: Convert with variable

One of these days, Klaus is going to answer a question before it is asked!

redfield
Posts: 66
Joined: Thu Apr 04, 2019 1:41 pm

### Re: Convert with variable

Thanks guys, can't believe that I didn't see this .

Have run into my next problem and again not getting it . Field fHours contains 10 and field fMinutes 15. I am expecting the output: 10:15 AM. But this code is giving me a strange output:

Code: Select all

put fld "fHours" & ":" & fld "fMinutes" & " AM" into varTime
Output:
Attachments
output.png (3.9 KiB) Viewed 1450 times

Klaus
Posts: 11872
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

### Re: Convert with variable

Script is coprrect, but are you really sure that fields "fHours" and "fMinutes" contain what you think they contain? Check for empty lines etc.

redfield
Posts: 66
Joined: Thu Apr 04, 2019 1:41 pm

### Re: Convert with variable

Weird, I thought I had checked this but seemingly there were still several carriage returns in one field. Looks okay now.