New date format: YYYY-MM-DD

Ken Ray kray at sonsothunder.com
Fri Oct 14 21:24:37 EDT 2005


On 10/14/05 5:53 PM, "Charles Hartman" <charles.hartman at conncoll.edu> wrote:

> Maybe a built-in date-conversion function, with different standards
> as arguments controlling the output? It would be *great* to have a
> smart function like that, that would take the whole range of formats
> various users might type into a field. (Anybody got one?)

Here you go! The function below has more comments than any other function
I've written <grin>.

Have fun!


Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com



WATCH FOR LINE WRAPS!

----------------------------------------------------------------------
--| FUNCTION: stsFormattedDate
--|
--| Author:   Ken Ray
--| Version:  2.0
--| Created:  8/2/04
--| Last Mod: 11/15/04
--| Requires: --
--|
--| Formats an incoming date according to a variety of date patterns and
--| outputs the
--| result. 
--|
--| Parameters:
--|     <pDate>: The date to be formatted. Must be a date or date/time
--|           combination, or empty. If empty, uses the current date and
--|           default 2AM time if time is requested in the pattern.
--|     <pFormat>: The pattern to use to format the incoming date. The
--|           following patterns are available where "M" is used for the
--|           month, "D" for the day, "Y" for the year, "W" for the weekday
--|           name, "H" for hours, "P" for AM/PM, "N" for minutes, "S" for
--|           seconds, and "G" for GMT calculation, as follows:
--|                M = month number, no leading zeroes (1-12)
--|                MM = month number, with leading zeroes (01-12)
--|                MMM = month name, abbreviated (Jan - Dec)
--|                MMMM = month name, long (January - December)
--|                D = day number, no leading zeroes (1-31)
--|                DD = day number, with leading zeroes (01-31)
--|                W = weekday name, single letter (S/M/T/W/t/F/s)
--|                WW = weekday name, shortest (Su/M/Tu/W/Th/F/Sa)
--|                WWW = weekday name, abbreviated (Sun/Mon/Tue/Wed/Thu/
--|                   Fri/Sat)
--|                WWWW = weekday name, long (Sunday/Monday/Tuesday/etc.)
--|                YY = two-digit year (00-99)
--|                YYYY = four digit year (1970-2004)
--|                H = hours, no leading zeroes, 12 hour format (1-12)
--|                HH = hours, leading zeroes, 12 hour format (01-12)
--|                HHH = hours, no leading zeroes, 24 hour format (1-23)
--|                HHHH = hours, leading zeroes, 24 hour format (01-23)
--|                HHHHH = military time format (0000 - 2359)
--|                P = AM/PM, single character, lower case (a/p)
--|                PP = AM/PM, single character, upper case (A/P)
--|                PPP = AM/PM, two characters, lower case (am/pm)
--|                PPPP = AM/PM, two characters, upper case (AM/PM)
--|                N = minutes, no leading zeroes (0-59)
--|                NN = minutes, leading zeroes (00-59)
--|                S = seconds, no leading zeroes (0-59)
--|                SS = seconds, leading zeroes (00-59)
--|                G = Calculation based on GMT (-1100 to +1100)
--|     <pUseBrackets>: Determines whether or not the format needs to have
--|           brackets surrounding each part of the pattern. If true, it
--|           requires that all patterns are surrounded by square brackets,
--|           and allows the letters used in the pattern (MDYWHNSGP) to be
--|           used as part of the return string that is *not* part of the
--|           pattern (for example "GMT" as a string). If false, pattern
--|           letters are replaced by the corresponding date parts in the
--|           returned string (so it assumes that characters that are not
--|           part of the pattern are not letters, but symbols).
--|
--| Examples:
--|    MM/DD/YYYY  -> 04/07/2004
--|    WWW, MMM D YYYY  -> Mon, Aug 2 2004
--|    WWW, MMM D YYYY G  --> Mon, Aug 2 2004 -0500
--|    [WWW],[MMM] [D] [YYYY] [G] GMT  --> Mon, Aug 2 2004 -0500 GMT
----------------------------------------------------------------------
function stsFormattedDate pDate,pFormat,pUseBrackets
  if (pDate = "") or (pDate = "Now") then put the date && \
    the long time into pDate
  if (pFormat = "") then put "MM/DD/YYYY" into pFormat
  if isNumber(word -1 of pDate) and (word -1 of pDate <=2359) then
    -- date and military time sent in, just needs a colon between hour
    -- and minute for it to be converted
    put ":" before char -2 of pDate
  end if
  -- Check for am/pm without preceding space
  put offset("am",pDate) into tLoc
  if (tLoc <> 0) and (char (tLoc-1) of pDate <> " ") then put " " before
char tLoc of pDate
  put offset("pm",pDate) into tLoc
  if (tLoc <> 0) and (char (tLoc-1) of pDate <> " ") then put " " before
char tLoc of pDate
  
  put ((pUseBrackets <> "") and (pUseBrackets <> "false")) into pUseBrackets
  put pDate into tOrigDate
  
  convert pDate to dateItems
  if (pDate is "invalid date") or (("69" is not in tOrigDate) and (item 1 of
pDate="1969")) then
    return "invalid date"
  end if
  put item 1 of pDate into tYear
  put item 2 of pDate into tMonthNum
  put item 3 of pDate into tDayNum
  put item 4 of pDate into tHour
  put item 5 of pDate into tMinute
  put item 6 of pDate into tSecond
  put item 7 of pDate into tWeekdayNum
  put word -1 of the internet date into G
  set the numberFormat to "00"
  
  if pUseBrackets then
    put "[MMMM],[MMM],[MM],[M],[WWWW],[WWW],[WW],[W],[DD],[D],[YYYY]," & \
      "[YY],[HHHHH],[HHHH],[HHH],[HH],[H],[PPPP],[PPP],[PP],[P],[NN]," & \
      "[N],[SS],[S],[G]" into tFormatWords
  else
    put "MMMM,MMM,MM,M,WWWW,WWW,WW,W,DD,D,YYYY," & \
      "YY,HHHHH,HHHH,HHH,HH,H,PPPP,PPP,PP,P,NN,N,SS,S,G" into tFormatWords
  end if
  put "!@#$%^&*()_+{}|:'<>?~`-=[]" into tReplaceChars
  repeat with x = 1 to the number of items of tFormatWords
    replace (item x of tFormatWords) with "[[[" & char x of tReplaceChars &
"]]]]" in pFormat
  end repeat
  
  put tYear into YYYY
  put char -2 to -1 of tYear into YY
  
  put tMonthNum into M
  put (tMonthNum+0) into MM
  put line tMonthNum of the abbreviated monthNames into MMM
  put line tMonthNum of the long monthNames into MMMM
  
  put tDayNum into D
  put (tDayNum+0) into DD
  
  put char tWeekDayNum of "SMTWtFs" into W
  put item tWeekDayNum of "S,M,Tu,W,Th,F,Sa" into WW
  put line tWeekDayNum of the abbreviated weekdayNames into WWW
  put line tWeekDayNum of the long weekdayNames into WWWW
  
  put tHour into HHH
  put (tHour+0) into HHHH
  if tHour <=12 then
    put tHour into H
    put "a" into P
    put "A" into PP
    put "am" into PPP
    put "AM" into PPPP
  else
    put tHour-12 into H
    if H < 10 then
      delete char 1 of H  -- remove leading 0
    end if
    put "p" into P
    put "P" into PP
    put "pm" into PPP
    put "PM" into PPPP
  end if
  put (H+0) into HH
  
  put tMinute into N
  put (tMinute+0) into NN
  
  put HHHH & NN into HHHHH
  
  put tSecond into S
  put (tSecond+0) into SS
  
  repeat with x = 1 to the number of items of tFormatWords
    if pUseBrackets then
      local tTemp
      get matchText(item x of tFormatWords,"\[(.*?)\]",tTemp)
      do "put" && tTemp && "into tVal"
    else
      do "put" && (item x of tFormatWords) && "into tVal"
    end if
    replace "[[[" & char x of tReplaceChars & "]]]]"  with tVal in pFormat
  end repeat
  
  return pFormat
end stsFormattedDate




More information about the use-livecode mailing list