WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Time UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Format Current Date and Time

Keywords: 	 format current date and time

;---------------------------------------------------------------
;
;  TimeFormat UDF....
;
;     NowFormat("Format")          Formats the current date and time
;     TimeFormat(YmdHms,"Format")  Formats the specified date and time.
;
;  The format string can contain any characters.  The following
;  characters are found and replaced as follows.  (Note that
;  these character strings are case sensitive.)
;
;    String      Is replaced with
; ----------   --------------------------------------------------
;    YYYY        Four digit year
;    YY          Two digit year
;    MMMM        Full name of the month
;    MMM         Three letter abbreviation of the month
;    MM          The month as a two digit number
;    DDDD        Full name of the day of the week.
;    DDD         Three letter abbreviation of the day of the week.
;    DD          Day of the month, as a two digit number.
;    HH          The hour, in 24 hour format, as a two digit number.
;    hh          The hour, in 12 hour format, as a two digit number.
;    mm          The minute as a two digit number
;    ss          Seconds as a two digit number.
;    AM          AM or PM  (upper case.)
;    am          am or pm. (lower case.)
;    ZM          Month as a zero-suppressed number.
;    ZD          Day of the month as a zero-suppressed number.
;    ZH          Hour in 24 hour format, zero-suppressed.
;    zh          Hour in 12 hour format, zero-suppressed.
;    zm          Minute, zero-suppressed.
;    zs          Seconds, zero-supressed.
;    JJJ         Julian day.  (Number of days since the beginning of the year.)
;
;   Any other characters in the format will be passed to the output
;   unchanged.  This is particularly useful for punctuation, such as
;   MM/DD/YY  HH:mm:ss
;
;----------------------------------------------------------------------------


#DefineFunction NowFormat(Format)
   Return TimeFormat(TimeYmdHms(),Format)
#EndFunction

#DefineFunction TimeFormat(YmdHms, Format)
  YY = ItemExtract (1,YmdHms,":")          ; Extract Year
  MM = ItemExtract (2,YmdHms,":")          ; Month
  DD = ItemExtract (3,YmdHms,":")          ; and day.
  if YY < 100 then YY = YY + 2000          ; Two digit years assumed to be 2000

  Hour24 = ItemExtract(4,YmdHms,":")       ; Extract Hour
  Minute = ItemExtract(5,YmdHms,":")       ; Minute
  Second = ItemExtract(6,YmdHms,":")       ; and second
  if Hour24 < 12
      Hour12 = Hour24                      ; Get 12-hour format hour
      AMPM   = "am"                        ; and AM/PM value
  else
      Hour12 = Hour24 - 12
      AMPM   = "pm"
  endif
  if Hour12 == 0 then Hour12 = 12
                                            ; Get julian day and day of week
  JJJ = TimeDiffDays(YmdHms,strcat(ItemExtract(1,YmdHms,":"),":01:01:00:00:00"))+1
  Weekday = ((TimeJulianDay(YmdHms)+5) mod 7) + 1
                                            ; Get month and day name
  MMMM = ItemExtract(MM,"January.February.March.April.May.June.July.August.September.October.November.December",".")
  DDDD = ItemExtract(Weekday,"Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday",".")
      
        ;-------------------------------------------------------------
        ; Copy the format to the buffer, looking for substititutions 
        ;-------------------------------------------------------------
     FX = 1
     LenFmt = StrLen(Format)
     Output = ""

     while @TRUE
         if FX > LenFmt then break
         Four = StrSub(Format,FX,4)
         Three = StrSub(Four,1,3)
         Two = StrSub(Three,1,2)
         New  = StrSub(Two,1,1)

         Skip = 1  
         if StrScan(New,"aAYMDHZhmszJ",1,@FWDSCAN)
          Select  @TRUE
            case Two == "am"
                Skip = 2
                New = AMPM
                break
            
            case Two == "AM"      
                 Skip = 2  
                 New = StrUpper(AMPM)
                 break

            case Four == "YYYY"
                skip = 4
                New = YY
                break

            case Two == "YY"
                skip = 3
                New = StrFixLeft(YY mod 100,"0",2)
                break

            case Four == 'MMMM'
                skip = 4
                New= MMMM
                break

            case Three == "MMM"
                skip = 3
                New = StrSub(MMMM,1,3)
                break

            case Two == "MM"
                skip = 3
                New = StrFixLeft(MM,"0",2)
                break

            case Four == "DDDD"
                skip = 4
                New = DDDD
                break

            case Three == "DDD"
                skip = 3
                New = StrSub(DDDD,1,3)
                break

            case Two == "DD"
                skip = 2
                New = StrFixLeft(DD,"0",2)
                break

            case Two == "HH" 
               skip = 2
               New = StrFixLeft(Hour24,"0",2)
               break
 
            case Two == 'hh'
               skip = 2
               New = StrFixLeft(Hour12,"0",2)
               break
                   
            case Two == 'mm' 
               skip = 2
               New = StrFixLeft(Minute,"0",2)
               break

            case Two == 'ss'
               skip = 2
               New = StrFixLeft(Second,"0",2)
               break
    
            case Two == "ZM"
                skip = 2
                New = MM+0
                break

            case Two == "ZD"
                skip = 2
                New = DD+0
                break

            case Two == "ZH"
                skip = 2
                New = Hour24+0
                break

            case Two == "zh"
                skip = 2
                New = Hour12+0
                break

            case Two == "zm"
                skip = 2
                New = Minute+0
                break

            case Two == "zs"
                skip = 2
                New = Second +0
                break

            case Three == "JJJ"
                skip = 3
                New = StrFixLeft(JJJ,"0",3)
                break
          
          end select
         endif
         
         Output = Strcat(Output,New)
         FX = FX + Skip
     endwhile
     return Output
#EndFunction


Article ID:   W15769
File Created: 2003:05:13:11:29:58
Last Updated: 2003:05:13:11:29:58