Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by BarrySumpter » Wed Aug 10, 2011 2:40 am

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
Last edited by BarrySumpter on Wed Aug 10, 2011 3:40 am, edited 1 time in total.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by BarrySumpter » Wed Aug 10, 2011 2:56 am

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
Last edited by BarrySumpter on Wed Aug 10, 2011 5:15 am, edited 2 times in total.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

wsamples
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 264
Joined: Mon May 18, 2009 4:12 am

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by wsamples » Wed Aug 10, 2011 4:00 am

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.
Last edited by wsamples on Wed Aug 10, 2011 4:16 am, edited 1 time in total.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by Dixie » Wed Aug 10, 2011 4:04 am

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

wsamples
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 264
Joined: Mon May 18, 2009 4:12 am

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by wsamples » Wed Aug 10, 2011 4:43 am

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?

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by Dixie » Wed Aug 10, 2011 4:50 am

Warren..

I guess it's 'horses for courses'... :) Nice thing about LiveCode, lots of ways to do something...

be well

Dixie

dglass
Posts: 519
Joined: Thu Sep 24, 2009 9:10 pm
Contact:

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by dglass » Wed Aug 10, 2011 5:02 am

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

wsamples
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 264
Joined: Mon May 18, 2009 4:12 am

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by wsamples » Wed Aug 10, 2011 5:10 am

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.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by BarrySumpter » Wed Aug 10, 2011 5:20 am

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.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

wsamples
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 264
Joined: Mon May 18, 2009 4:12 am

Re: Is there cooler LC way to format dates of 1 - 9 to 01 - 09?

Post by wsamples » Wed Aug 10, 2011 8:28 am

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

Post Reply