DateItems to the nth

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

hamlynart
Posts: 101
Joined: Wed Mar 19, 2008 7:10 pm
Contact:

DateItems to the nth

Post by hamlynart » Fri Mar 27, 2009 2:52 pm

Hi Folks,

Any Ideas of a way to get:

Friday, March 27th, 2009

etc

instead of

Friday, March 27, 2009

ie: how do I get the "st", "nd," "rd" and "th" suffixes to be appended?

Thanks in advance.

Jim H

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: DateItems to the nth

Post by sturgis » Fri Mar 27, 2009 3:34 pm

I suspect you'd have to build the string yourself, or use a replace on an existing string.

I think it might be easiest to do the self build method
Get the day number in question, check to see what the last character is.

If its a 1, tack st on to it. If its a 2, nd, if its a 3, rd. everything else, tack on the th.

Then grab the day name, month name, and year and "put" it all together into whatever form you want.

I haven't messed with date functionality much yet, and am still extremely new to all this, but if you have trouble getting things to work I can try to put something together that will do.
hamlynart wrote:Hi Folks,

Any Ideas of a way to get:

Friday, March 27th, 2009

etc

instead of

Friday, March 27, 2009

ie: how do I get the "st", "nd," "rd" and "th" suffixes to be appended?

Thanks in advance.

Jim H

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Mar 27, 2009 3:40 pm

Hi Jim,

Sturgis is right, you need to do this yourself. The following should get you going:

Code: Select all

put the long date into myDate
put last word of item 2 of myDate into myDay
if last char of myDay is 1 then
  put "st" after myDay
else if last char of myDay is 2 then
  put "nd" after myDay
else if last char of myDay is 3 then
  put "rd" after myDay
else
  put "th" after myDay
end if
put myDay into last word of item 2 of myDate
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

hamlynart
Posts: 101
Joined: Wed Mar 19, 2008 7:10 pm
Contact:

Post by hamlynart » Fri Mar 27, 2009 5:45 pm

Thanks to you both. I was hoping there would be a more elegant way.

Just a few changes to your code works perfectly though (11th 12th and 13th need spacial attention):

Code: Select all

on mouseUp
   put the long date into myDate 
   put last word of item 2 of myDate into myDay
   if myDay is 11 then
      put "th" after myDay
   else if myDay is 12 then
      put "th" after myDay 
   else if myDay is 13 then
      put "th" after myDay 
   else if last char of myDay is 1 then 
      put "st" after myDay 
   else if last char of myDay is 2 then 
      put "nd" after myDay 
   else if last char of myDay is 3 then 
      put "rd" after myDay 
   else
      put "th" after myDay 
   else
      put "th" after myDay 
   end if 
   put myDay into last word of item 2 of myDate
   put myDate into fld "Field"
end mouseUp
Cheers

Jim H

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Mar 27, 2009 5:51 pm

Hi Jim,

I forgot about 11-12-13. You could use:

Code: Select all

if myDay is among the items of "11,12,13" then
That saves you a few lines.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 157
Joined: Thu Jun 29, 2006 4:16 pm

Post by Randy Hengst » Fri Mar 27, 2009 8:36 pm

How about something like this:

on mouseUp
switch the last char of myDay
case 1
put "st" after myDay
break
case 2
put "nd" after myDay
break
case 3
put "rd" after myDay
break
case 4
case 5
case 6
case 7
case 8
case 9
case 0
put "th" after myDay
break
end switch
end mouseUp

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Fri Mar 27, 2009 8:57 pm

This would work too, but would need some if's for case 1,2 and 3 as above to handle 11, 12, and 13. Or a nested switch I guess.

Think I read somewhere in the forums about efficiency of switch vs if blocks

In this case my guess is they'd be comparable, the way the if statements above are written, with else if rather than separate if blocks for each possibility, the whole if structure wouldn't have be be evaluated line by line as apposed to separate if blocks.

Same for switch/case. Soon as a match is found, exit switch is hit and the rest of the cases don't have to be evaluated.

Is my understanding of how these things are evaluated correct?
Randy Hengst wrote:How about something like this:

on mouseUp
switch the last char of myDay
case 1
put "st" after myDay
break
case 2
put "nd" after myDay
break
case 3
put "rd" after myDay
break
case 4
case 5
case 6
case 7
case 8
case 9
case 0
put "th" after myDay
break
end switch
end mouseUp

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 157
Joined: Thu Jun 29, 2006 4:16 pm

Post by Randy Hengst » Fri Mar 27, 2009 9:47 pm

Sorry I didn't pay attention 11, 12, 13..... What about this...

on mouseUp
switch the last char of myDay
case 1
if myDay = 1 then
put "st" after myDay
else
put "th" after myDay
end if
break
case 2
if myDay = 2 then
put "nd" after myDay
else
put "th" after myDay
end if
break
case 3
if myDay = 3 then
put "rd" after myDay
else
put "th" after myDay
end if
break
case 4
case 5
case 6
case 7
case 8
case 9
case 0
put "th" after myDay
break
end switch
end mouseUp

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Fri Mar 27, 2009 10:00 pm

Looks like that covers all bases, but you'd need to put code into each case for 4 through 0 at the end to get the desired result. Not sure tho, havn't tried it.

I think what you were looking for, and a more compact version could be something like the code below.
The code seems to works pretty well, would just have to use the alternate var that is holding the day number rather than my repeat loop.
Got the "among the items of" syntax from above and applied it to the switch.

There is a way to use a case default for things that don't match anything else but for some reason I couldn't figure out how to make that work for the 4 through 0 items, so think i'm gonna dig through the dictionary some more and figure out what I was doing wrong.

Code: Select all

on mouseUp
   put "" into field "Field"
   repeat with i = 1 to 31
      switch 
         case i is among the items of "11,12,13"
            put i & "th " after field "Field"
            exit switch
         case the last char of i is among the items of  "4,5,6,7,8,9,0"
            put i & "th " after field "Field"
            exit switch
         case the last char of i is "1"
            put i & "st " after field "Field"
            exit switch
         case the last char of i is "2"
            put i & "nd " after field "Field"
            exit switch
         case the last char of i is "3"
            put i & "rd " after field "Field"
            exit switch
      end switch
   end repeat
end mouseUp
Randy Hengst wrote:Sorry I didn't pay attention 11, 12, 13..... What about this...

on mouseUp
switch the last char of myDay
case 1
if myDay = 1 then
put "st" after myDay
else
put "th" after myDay
end if
break
case 2
if myDay = 2 then
put "nd" after myDay
else
put "th" after myDay
end if
break
case 3
if myDay = 3 then
put "rd" after myDay
else
put "th" after myDay
end if
break
case 4
case 5
case 6
case 7
case 8
case 9
case 0
put "th" after myDay
break
end switch
end mouseUp
Last edited by sturgis on Fri Mar 27, 2009 10:07 pm, edited 1 time in total.

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 157
Joined: Thu Jun 29, 2006 4:16 pm

Post by Randy Hengst » Fri Mar 27, 2009 10:03 pm

OK, so I obviously forgot to include the 11, 12, 13 in the .... reverse my ifs...

if myDay = 11 then
put "th" after myDay
else
put "st" after myDay
end if
break
case 2
if myDay = 12 then
put "th" after myDay
else
put "nd" after myDay
end if
break
case 3
if myDay = 13 then
put "th" after myDay
else
put "rd" after myDay
end if
break

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Fri Mar 27, 2009 10:21 pm

yeah, its me again. Figured out that I was trying to make the default case way too complicated.

Heres the code, eliminating one whole line. lol

Code: Select all

on mouseUp
   put "" into field "Field"
   repeat with i = 1 to 31
      switch 
         case i is among the items of "11,12,13"
            put i & "th " after field "Field"
            exit switch
         case the last char of i is "1"
            put i & "st " after field "Field"
            exit switch
         case the last char of i is "2"
            put i & "nd " after field "Field"
            exit switch
         case the last char of i is "3"
            put i & "rd " after field "Field"
            exit switch
         default
            put i & "th " after field "Field"
      end switch
   end repeat
end mouseUp
Heres Marks code from above, merged with his addendum for the 11th, 12th, and 13th.

Code: Select all

put the long date into myDate
put last word of item 2 of myDate into myDay
if myDay is among the items of "11,12,13" then
   put "th" after myDay
else if last char of myDay is 1 then
  put "st" after myDay
else if last char of myDay is 2 then
  put "nd" after myDay
else if last char of myDay is 3 then
  put "rd" after myDay
else
  put "th" after myDay
end if
put myDay into last word of item 2 of myDate

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Sat Mar 28, 2009 4:30 am

Decided to try another way to do this sort of thing. Not the best way, both the switch and if blocks work well but.. I was bored.

Code: Select all

local appnd
put "" into appnd
function fillArray tTmp theAppend
   repeat for each item myItem in tTmp 
      put theAppend into appnd[myItem] 
   end repeat
end fillArray

on mouseUp
   do fillArray("4,5,6,7,8,9,0,10,11,12,13,14,15,16,17,18,19,20,24,25,26,27,28,29,30,31","th")
   do fillArray ("1,21,31","st")
   do fillArray ("2,22","nd")
   do fillArray ("3,23","rd")
   put "" into field "Field"
   repeat with i = 1 to 31
      put i & appnd[i] & space after field "Field"
   end repeat 
end mouseUp
The fillArray lines would only have to be done somewhere once, (make it global?) at which point, to get the day with 2 letter addition is just put myDay & appnd[myDay]

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Mon Mar 30, 2009 10:37 pm

I came up with this:

Code: Select all

function ordinate pNum
   if (char -1 of pNum is in "0456789") or (char -2 of pNum is "1") then
      put "th" into tSuffix
   else
      put item (char -1 of pNum) of "st,nd,rd" into tSuffix
   end if
   return pNum & tSuffix
end ordinate

Best,

Mark

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Tue Mar 31, 2009 1:07 am

Ok, thats just cool.
Mark Smith wrote:I came up with this:

Code: Select all

function ordinate pNum
   if (char -1 of pNum is in "0456789") or (char -2 of pNum is "1") then
      put "th" into tSuffix
   else
      put item (char -1 of pNum) of "st,nd,rd" into tSuffix
   end if
   return pNum & tSuffix
end ordinate

Best,

Mark

rtgmath
Posts: 2
Joined: Tue Jul 22, 2008 6:49 am
Contact:

Post by rtgmath » Wed Apr 01, 2009 4:49 am

How about this?

Code: Select all

function datesuffix n
	put "th" into mysuffix
	if n <=3 or (n>=21 and n<=23) then
		put item n mod 10 of "st,nd,rd" into mysuffix
	end if
	return mysuffix
end datesuffix
You do what you do because you are what you are. This is true in math, in people, and in programming. But since people have free will, we can change what we are, and hence, what we do.

Post Reply