Page 1 of 1
Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 2:40 am
by BarrySumpter
Some of my dates using these routines are coming up as:
2011-08-1
instead of
2011-08-01
Is there a cool way in LiveCode to zero fill format the 1 to an 01
WITHOUT using the assembler type notation of format()?
Code: Select all
-- any thing cooler than:
if the Length of item 1 of pAUSdate is 1 then
put "0" & item 1 of pAusDate into Item 1 of pAUSDate
end if
Code: Select all
Function ConvertAUSDateToSQLDate pAUSDate
-- dd/mm/yyyy to yyyy-mm-dd
set the itemdelimiter to slash
put item 3 of pAUSDate & "-" & item 2 of pAUSDate & "-" & item 1 of pAUSDate into theDate
return theDate
end ConvertAUSDateToSQLDate
Function ConvertUSDateToSQLDate pUSDate
-- mm/dd/yyyy to yyyy-mm-dd
set the itemdelimiter to slash
put item 3 of pUSDate & "-" & item 1 of pUSDate & "-" & item 2 of pUSDate into theDate
Return theDate
end ConvertUSDateToSQLDate
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 2:56 am
by BarrySumpter
set the numberFormat to "00"
Put 1 + 0
nope doesn't work.
edit:
Sorry should have qualified this.
the numberFormat DOES affect the IT when using IT to PUT to the message box
But not when using a local variable
Put 1 + 0 into myVar
I wonder now:
set the numberFormat of myVar to "00" -- probably errors
or
set the numberFormat of fielkd txtTotal to "00" -- might work
hmmm
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 4:00 am
by wsamples
Barry, you can use:
'if myNum < 10 then put "0" before myNum'
as a single line "if" statement. Your method, as clever as it is, simply replaces that "if" with a calculation, thus it doesn't appear to save you anything.
--- EDIT:
I somehow didn't see your comment that it doesn't work, although I believe it does work. If you enter this, for example, into the message box:
set the numberFormat to "00";put 8 + 0
it will return 08. It does what you are seeking for any values whose sum < 10 while having no effect when the sum is > 10. You should be able to put your date into the equation, add zero as per your example, and come up with your two digit number, zero padded as necessary. Try in the message box:
put 2 into myNum;set the numberformat to "00";put myNum + 0
and
put 12 into myNum;set the numberformat to "00";put myNum + 0
It works here, I just don't see it as saving you much if any typing, although I salute your inventiveness.
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 4:04 am
by Dixie
hi..
Code: Select all
on mouseUp
put "2011/08/1" into bruce
put ConvertAUSDateToSQLDate(bruce)
end mouseUp
function ConvertAUSDateToSQLDate pAUSDate
set the itemDel to slash
put item 3 of pAUSDate & "-" & item 2 of pAUSDate & "-" & item 1 of pAUSDate into theDate
set itemDel to "-"
if the number of chars of item 1 of theDate = 1 then
put "0" & item 1 of theDate into item 1 of theDate
end if
set itemDel to comma
return theDate
end ConvertAUSDateToSQLDate
might help...
Dixie
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 4:43 am
by wsamples
Actually, looking at the complete function supplied by Dixie, I think if you go back to your thought of using numberFormat you may find you were headed on the right path after all... Try this in the multi-line message box and see if this doesn't give you an idea
put "12/2/2" into tDate
set the itemDel to slash
set the numberFormat to "00"
put item 1 of tDate & "-" & (item 2 of tDate + 0) & "-" & (item 3 of tDate + 0)
It seems like it saves some steps, no?
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 4:50 am
by Dixie
Warren..
I guess it's 'horses for courses'...

Nice thing about LiveCode, lots of ways to do something...
be well
Dixie
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 5:02 am
by dglass
or ...
Code: Select all
function gmcSQLDate pDateValue
convert pDateValue to dateitems
--put it into tDateItems
put item 1 of pDateValue & "-" & character -2 to -1 of ("00" & item 2 of pDateValue) & "-" & character -2 to -1 of \
("00" & item 3 of pDateValue) into tSQLDate
return tSQLDate
end gmcSQLDate
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 5:10 am
by wsamples
Barry was looking for something different, and his was a completely novel approach for me. It's not an obvious "straight line" approach but in the end it seems to be quite efficient, at least in this scenario.
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 5:20 am
by BarrySumpter
wsamples wrote:
...
'if myNum < 10 then put "0" before myNum'
...
That the most readable with the least amount of words.
For me anyway.
Code: Select all
if item 1 of pAUSdate < 10 then put "0" before item 1 of pAUSdate
When using the
set the numberFormat to "00"
That silly put to message box doesn't give the same results when put into myLocalVar or put into field txtTotal.
Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?
Posted: Wed Aug 10, 2011 8:28 am
by wsamples
BarrySumpter wrote:
When using the
set the numberFormat to "00"
That silly put to message box doesn't give the same results when put into myLocalVar or put into field txtTotal.
That struck me as curious, so...
Putting this into a button seems to work as expected:
on mouseUp
put field "date" into tDate
set the itemdel to slash
set the numberformat to "00"
put item 1 of tDate & "-" & (item 2 of tDate + 0) & "-" & (item 3 of tDate + 0) into field "converted date"
end mouseUp
(Field "date" contains the date without zero padding, e.g., 1999/2/2 and field "converted date" contains the newly formatted date.)
Another button which calls a function also seems to work as expected:
on mouseUp
put field "date" into tDateToConvert
put convertDate(tDateToConvert) into field "converted date"
end mouseUp
function convertDate tDate
set the itemdel to slash
set the numberformat to "00"
put item 1 of tDate & "-" & (item 2 of tDate + 0) & "-" & (item 3 of tDate + 0) into tConvertedDate
return tConvertedDate
end convertDate