Page 1 of 1

Date manipulation oddity

Posted: Wed Oct 23, 2013 10:00 am
by nicoloose
I have two date fields and I want to convert them to a format of "YYYY-MM-DD" and am successful in converting the first date but when the code comes to convert the second date, it gives a very weird result.

put empty into tStart
put empty into fDate
put field "startDate" into fDate
convert fDate to dateitems
put (the item 1 of fDate) & "-" & (the item 2 of fDate) & "-" & (the item 3 of fDate) into tStart

set the itemDelimiter to "-"
put format("%02s", item 2 of tStart) into item 2 of tStart
put format("%02s", item 3 of tStart) into item 3 of tStart

****** RESULT = 2013-01-01 *******

put empty into tEnd
put field "endDate" into eDate
convert eDate to dateitems
put (the item 1 of eDate) & "-" & (the item 2 of eDate) & "-" & (the item 3 of eDate) into tEnd

set the itemDelimiter to "-"
put format("%02s", item 2 of tEnd) into item 2 of tEnd
put format("%02s", item 3 of tEnd) into item 3 of tEnd

******** RESULT = 2013,1,31,0,0,0,5-00-00 ********

If I swap the two pieces of code around, the same things happens. Always the second date that has this issue?

Re: Date manipulation oddity

Posted: Wed Oct 23, 2013 11:48 am
by bangkok
Ouh la la... don't bother... keep it simple ! :)

From Date&Time library of Sarah.

http://www.troz.net/rev/stacks/DateTime.rev

Code: Select all

function englishToSQLdate pDate
   if pDate is empty then put the short english date into pDate
   convert pDate from short english date to dateItems
   put item 2 of pDate into m
   if m < 10 then put "0" before m
   put item 3 of pDate into d
   if d < 10 then put "0" before d
   return item 1 of pDate & "-" & m & "-" & d
end englishToSQLdate()
on mouseup
answer englishToSQLdate ()
end mouseup

Re: Date manipulation oddity

Posted: Wed Oct 23, 2013 12:03 pm
by bn
Hi Nicoloose,

your second date does not work because you forgot to set the itemDelimiter to "," after the first conversion and before the second conversion.

It should work if you put

Code: Select all

set the itemDelimiter to ","
before

Code: Select all

put empty into tEnd
Kind regards
Bernd

Re: Date manipulation oddity

Posted: Thu Oct 24, 2013 11:02 am
by nicoloose
Simplicity and logic are not among the lines of the things in me!
Thanks for the help!

Re: Date manipulation oddity

Posted: Thu Oct 24, 2013 5:30 pm
by jacque
Simplicity and logic are not among the lines of the things in me!
LOL! But you learn fast. :)