DateItems to the nth
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
DateItems to the nth
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
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
Re: DateItems to the nth
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.
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
Hi Jim,
Sturgis is right, you need to do this yourself. The following should get you going:
Best,
Mark
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
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):
Cheers
Jim H
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
Jim H
Hi Jim,
I forgot about 11-12-13. You could use:
That saves you a few lines.
Best,
Mark
I forgot about 11-12-13. You could use:
Code: Select all
if myDay is among the items of "11,12,13" then
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- VIP Livecode Opensource Backer
- Posts: 157
- Joined: Thu Jun 29, 2006 4:16 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?
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
-
- VIP Livecode Opensource Backer
- Posts: 157
- Joined: Thu Jun 29, 2006 4:16 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
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
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 157
- Joined: Thu Jun 29, 2006 4:16 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
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
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
Heres Marks code from above, merged with his addendum for the 11th, 12th, and 13th.
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
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
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.
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]
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
-
- Posts: 179
- Joined: Sat Apr 08, 2006 11:08 pm
- Contact:
I came up with this:
Best,
Mark
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
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
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.