Page 1 of 1
Having some issues with "convert" (RESOLVED)
Posted: Tue Oct 08, 2013 1:21 pm
by NigelS
Greetings
I'm not sure if this is a bug or not but I'm getting an error when I try the following.
I have two dates that I want to calculate the no of days elapsed. The dates are as follows
Date 1 = 31/08/2012
Date 2 = 10/08/2013
When I try and use 'convert" I get an error on date 1 as been incorrect. The difference is in the year, if I use the current year then no error.
I've tried the various 'convert' syntax's but still no joy.
I'm using Community 6.1 for Windows.
Any ides?
Re: Having some issues with "convert"
Posted: Tue Oct 08, 2013 1:40 pm
by bangkok
The very good "Date & Time Library"
here :
http://www.troz.net/rev/index.irev?cate ... ary#stacks
... will do the trick (and will teach you how to deal with convert)
look for "daysBetween" function
Re: Having some issues with "convert"
Posted: Tue Oct 08, 2013 1:45 pm
by dunbarx
Hi.
The dateformat may be the culprit. In the civilized world, the date "31/08/2013" is usually written "08/31/2013" and this gives a valid conversion. You can modify this format, though into Martian if you wish.
Code: Select all
on mouseUp
--get "31/08/2012" --works on Mars
get "3/08/2012" --works on Earth
convert it to seconds
answer it
end mouseUp
Craig Newman
Re: Having some issues with "convert"
Posted: Tue Oct 08, 2013 2:21 pm
by NigelS
Greetings
I have had a look at dayBetween and extrabulted this function and this is where I picked up that the year caused a problem.
This is the script I wrote to calculate the time difference
Code: Select all
# -------
function CalcTimeElapsed pStartTime pEndTime
local tCalculatedElapsedTime
local tHour,tMin
local tHourInSec, tMinInSec
convert pStartTime to dateItems -- LastMsgUpDateTime
convert pEndTime to dateItems -- CurrentTime
-- this'll calculate the elapsed time
subtract item 4 of pStartTime from item 4 of pEndTime
subtract item 5 of pStartTime from item 5 of pEndTime
put item 4 of pEndTime into tHour
put item 5 of pEndTime into tMin
-- convert into sec's
put tHour*60*60 into tHourInSec
put tMin*60 into tMinInSec
put tHourInSec+tMinInSec into tCalculatedElapsedTime
return tCalculatedElapsedTime
end CalcTimeElapsed
# -------
This works fine if I ignore the year.
Craig - are you saying that I can't calculate for any day that exist with 2 digits? The format I'm using is mm/dd/yy, should I use a different format?
Re: Having some issues with "convert"
Posted: Tue Oct 08, 2013 2:36 pm
by bn
Hi Craig,
In the civilized world, the date "31/08/2013" is usually written "08/31/2013" and this gives a valid conversion. You can modify this format, though into Martian if you wish.
greetings from Mars to the civilized world
Kind regards
Bernd
Re: Having some issues with "convert"
Posted: Tue Oct 08, 2013 2:46 pm
by dunbarx
No, I just meant that the "dateformat" has to match whatever "date format" you are using. In the U.S., the format is "mm/dd/yy". But if you use a string that goes out of range, that is, the month is greater than 12, for example, the convert command will not do anything at all:
convert "31/08/2012" to dateItems --yields "31/08/2012"
convert "08/31/2012" to dateItems -- yields "2012,8,31,0,0,0,6"
In fact, any unresolvable string will be ignored by the convert command:
convert "08/////31/2012" to dateItems --yields "08/////31/2012"
convert "08/xx/2012" to dateItems --yields "08/xx/2012"
This is in contrast to the "dateItems", which is far more forgiving, and more powerful, in that you modify its items, and it will do the math to make it whole:
Code: Select all
on mouseUp
get the date
convert it to dateItems
add 15 to item 3 of it
convert it to date
end mouseUp
Craig
Re: Having some issues with "convert"
Posted: Tue Oct 08, 2013 5:02 pm
by NigelS
Okay... I'm with you now Craig. Here in South Africa our system dates are set to dd/mm/yy but recently a lot of business are switching to mm/dd/yy. This does cause some problems as we always have check first what the system date is set to and convert first. Should have thought of that.
Thx
Nigel
Re: Having some issues with "convert"
Posted: Tue Oct 08, 2013 5:15 pm
by Klaus
Hi Nigel,
can't you use "system date"?
Tried this here with german date format and worked like a charm:
...
## SYSTEM DATE is actually -> 08.10.13, but changing to 2013 also works!
convert "08.10.2013" from system date to dateitems
put it
## As exspected -> 2013,10,8,0,0,0,3
...
Best
Klaus
Re: Having some issues with "convert"
Posted: Tue Oct 08, 2013 6:34 pm
by NigelS
Klaus
Absolutely, it's something I should have checked first. All I can say is one word "Gotcha". First thing I'll do when I get into the office is try it out. Although I do use yyyy rather than yy. I think my problem lays with with Craig makes mention in his post.
Nigel
Re: Having some issues with "convert" (RESOLVED)
Posted: Wed Oct 09, 2013 7:59 am
by NigelS
Greetings

Always check your code. I found my problem and thought I would share what I had done.
The code
Code: Select all
-- format the current date
put tCurrentMonth &"/"& tCurrentDay &"/"& tCurrentYear into tTheCurrentDateTime
put tTheCurrentDateTime && tCurrentTime into tTheCurrentDateTime
put item 1 of tLastMsgUpdate into tTheLastMsgUpdateDate
put item 2 of tLastMsgUpdate into tTheLastMsgUpdateTime
-- extrabulate the last highsite message date
replace "-" with space in tTheLastMsgUpdateDate
put item 1 of tTheLastMsgUpdateDate into tMsgYear
put item 2 of tTheLastMsgUpdateDate into tMsgDay
put PrefixAZero( tMsgDay ) into tMilSeconds
put item 3 of tTheLastMsgUpdateDate into tMsgMonth
put tMsgMonth &"/"& tMsgDay &"/"& tMsgYear into tTheLastMsgUpdate
the problem lay in these line of code
Code: Select all
put item 2 of tTheLastMsgUpdateDate into tMsgDay
put PrefixAZero( tMsgDay ) into tMilSeconds
put item 3 of tTheLastMsgUpdateDate into tMsgMonth
I swapped item 2 and item 3 around where Item 2 should be tMsgMonth and Item 3 should be tMsgDay.
So, the moral of the story - Check you code
Thanks to all who helped with tips and hints.