Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: how do I get ampm format for time?

From: carl:cybercraft at: 19-Jan-2001 22:08

Hi Bob, You can extract hours, minutes and seconds from a time value...
>> a-time: 17:40
== 17:40
>> a-time/hour
== 17
>> a-time/minute
== 40 which can make things simplier. So... am-pm: func [time] [ ap: to-string time hour: time/hour either hour < 12 [ap: join ap "AM"][ap: join ap "PM"] if hour > 12 [replace ap to-string hour to-string (hour - 12)] if hour = 0 [replace ap to-string hour to-string 12] return ap ] This seems to work, though I'm sure others can improve on it. On 19-Jan-01, Bob Racko wrote:
> how do I format dates to am/pm format > like: "hh:mm pm EST" > is there a more succinct way to write this? > time-ampm: func [ dt ] > [ rejoin [ > ; handle hh:mm part > ( replace ( copy/part ( > rejoin [ > (either dt/time < 10:00 [ "0" ] [ "" ]) ; hh:mm vs [h]h:mm > either dt/time > 12:59:59 > [ mold dt/time - 12:00 ] > [ mold dt/time ] > ] > ) 5 ) "00:" "12:" ) ; midnite-1am is special > ; handle " pm EST" parts > either dt/time > 11:59:59 > [ " pm" ] > [ " am" ] > " EST" > ]] > yes this does the job, perhaps a mold could be factored out > and some parens are not needed. now the challenge > is to write something that handles the exceptions > with less code. this version allows coding by deletion > since the two parts hh:mm vs am/pm indicator > are treated separately and simply concatenated.
-- Carl Read [carl--cybercraft--co--nz]