Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Excel date formatting
Message
From
11/02/2003 12:50:37
 
 
To
10/02/2003 14:44:12
Mike Yearwood
Toronto, Ontario, Canada
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00751583
Message ID:
00751952
Views:
18
Mike,

I just wrote a program a couple of weeks ago that does this. The code is not pretty, but it is very handy since it supports (almost) any date/time pattern in any sequence and with any delimiters.

Here's the code:
*) Program...........: cDateTime.prg
*  Author............: Daniel Gramunt
*  Project...........: Common
*  Created...........: 01.02.2003 - 20:01:11 (Visual FoxPro 07.00.0000.9465)
*  Copyright.........: (c) Nokia, 2003
*) Description.......: Returns a date and/or time as a formatted character
*)                   : string based on the pattern defined with the parameter
*)                   : < tcFormat >
*)                   : 
*)                   : This program supports a wide range of return values
*)                   : and supports any delimiters. This program looks for
*)                   : each known pattern and replaces the pattern with the
*)                   : actual value. Any character string in < tcFormat > 
*)                   : that doesn't match a DateTime pattern, is left in
*)                   : the return value. This allows for any combination of
*)                   : delimiters.
*)                   : 
*  Calling Samples...: cDateTime("DD-MMM-YYYY")
*                    : cDateTime("DD-MMM-YYYY", {^2002.12.31})
*                    : 
*  Parameter List....: tcFormat - Defines the pattern of the DateTime character
*                    :            to return. The following patterns are 
*                    :            supported:
*                    : 
*                    :            Year
*                    :             YYYY: 1999, 2000, etc.
*                    :             YY:   99, 00, etc.
*                    : 
*                    :            Month
*                    :             MM:     01, 02, 03, etc.
*                    :             M:      1, 2, 3, etc.
*                    :             Mmm:    Jan, Feb, Mar, etc.
*                    :             MMM:    JAN, FEB, MAR, etc
*                    :             Mmmmm:  January, February, etc.
*                    :             MMMMM:  JANUARY, FEBRUARY, etc.
*                    : 
*                    :            Day
*                    :             DD:      01, 20, 30, etc.
*                    :             D:       1, 20, 30, etc.
*                    :             Ddd:     Mon, Tue, Wed, etc.
*                    :             DDD:     MON, TUE, WED, etc.
*                    :             Ddddd:   Monday, Tuesday, etc.
*                    :             DDDDD:   MONDAY, TUESDAY, etc.
*                    : 
*                    :            Hour
*                    :             HH:     01, 02, 03, etc.
*                    :             H:      1, 2, 3, etc.
*                    : 
*                    :            Minute (we cannot use M since it's used for Month)
*                    :             LL:     01, 02, 03, etc.
*                    :             L:      1, 2, 3, etc.
*                    : 
*                    :            Seconds
*                    :             SS:     01, 02, 03, etc.
*                    :             S:      1, 2, 3, etc.
*                    : 
*                    : [tdDateTime] - Datetime to use for return value. Optional.
*                    :                If omitted, uses the current DATETIME():
*  Major change list.: 01.02.2003 DGR: Rewritten in order to handle any combination of patterns, 
*                    :                 sequence and delimiters.
*--------------------------------------------------------------------------------------------------
LPARAMETERS tcFormat, ttDateTime

ASSERT VARTYPE(tcFormat) = "C";
       MESSAGE "Parameter < tcFormat > : Parameter missing or wrong type (Expecting 'C')"

LOCAL ltDateTime, lcDateTime, lcYYYY, lcYY
LOCAL lcMM, lcM, lcMm_, lcMMM, lcMmmm_, lcMMMMM
LOCAL lcDD, lcD, lcDd_, lcDDD, lcDddd_, lcDDDDD
LOCAL lcHH, lcH, lcLL, lcL, lcSS, lcS, lcRetVal

ltDateTime = IIF(VARTYPE(ttDateTime) = "T", ttDateTime,;
              IIF(VARTYPE(ttDateTime) = "D", DTOT(ttDateTime),;
                  DATETIME();
                  );
                 )
                 
lcDateTime = TTOC(ltDateTime, 1)

*-- first we save all the possible character expressions for each pattern
*-- into memvars 

*-- year
lcYYYY  = LEFT(lcDateTime, 4)                && 1999, 2000, etc.
lcYY    = SUBSTR(lcDateTime, 3, 2)           && 99, 00, etc.

*-- month
lcMM    = SUBSTR(lcDateTime, 5, 2)           && 01, 02, 03, etc.
lcM     = ALLTRIM(STR(MONTH(ltDateTime)))    && 1, 2, 3, etc.
lcMm_   = LEFT(CMONTH(ltDateTime), 3)        && Jan, Feb, Mar, etc.
lcMMM   = UPPER(lcMm_)                       && JAN, FEB, MAR, etc
lcMmmm_ = CMONTH(ltDateTime)                 && January, February, etc.
lcMMMMM = UPPER(lcMmmm_)                     && JANUARY, FEBRUARY, etc.

*-- day
lcDD    = SUBSTR(lcDateTime, 7, 2)           && 01, 20, 30, etc.
lcD     = ALLTRIM(STR(DAY(ltDateTime)))      && 1, 20, 30, etc.
lcDd_   = LEFT(CDOW(ltDateTime), 3)          && Mon, Tue, Wed, etc.
lcDDD   = UPPER(lcDd_)                       && MON, TUE, WED, etc.
lcDddd_ = CDOW(ltDateTime)                   && Monday, Tuesday, etc.
lcDDDDD = UPPER(lcDddd_)                     && MONDAY, TUESDAY, etc.

*-- hours
lcHH = SUBSTR( TTOC(DATETIME(), 1), 9, 2)
lcH = ALLTRIM(STR(VAL(lcHH)))

*-- minutes
*-- We have to use another character since M is used for month.
*-- We use L (Lepta (greek))
lcLL = SUBSTR( TTOC(DATETIME(), 1),11, 2)
lcL  = ALLTRIM(STR(VAL(lcMM)))

*-- seconds
lcSS = SUBSTR( TTOC(DATETIME(), 1),13, 2)
lcS = ALLTRIM(STR(VAL(lcSS)))

*-- now we overwrite each known pattern in < tcFormat > with
*-- the corresponding datetime values that we have saved into memvars.
*-- The idea is to use STRTRAN() which only replaces the known
*-- patterns. Anything else (delimiters) are left untouched.
*-- E.g.
*-- DD_MM_YYYY   => 31_12_2002
*-- DD-MMM-YYYYY => 21-DEC-2002

lcRetVal = tcFormat

*-- hours
lcRetVal = STRTRAN(lcRetVal, "HH", lcHH, 1, -1, 1)       && case-insensitive
lcRetVal = STRTRAN(lcRetVal, "H", lcH, 1, -1, 1)         && case-insensitive

*-- minutes
lcRetVal = STRTRAN(lcRetVal, "LL", lcLL, 1, -1, 1)       && case-insensitive
lcRetVal = STRTRAN(lcRetVal, "L", lcL, 1, -1, 1)         && case-insensitive

*-- seconds
lcRetVal = STRTRAN(lcRetVal, "SS", lcSS, 1, -1, 1)       && case-insensitive
lcRetVal = STRTRAN(lcRetVal, "S", lcS, 1, -1, 1)         && case-insensitive

*-- year

IF "YYYY" $ UPPER(tcFormat)
   lcRetVal = STRTRAN(lcRetVal, "YYYY", lcYYYY, 1, -1, 1)   && case-insensitive
ELSE
   IF "YY" $ UPPER(tcFormat)
      lcRetVal = STRTRAN(lcRetVal, "YY", lcYY, 1, -1, 1)       && case-insensitive
   ENDIF
ENDIF

*-- Month and Day patterns need some special handling since these formats
*-- may contain letters if the format is CDOW() or CMONTH().
*-- This could cause problems e.g. Saturday contains a "D" which
*-- would be interpreted as if a D was passed as < tcFormat >
*-- Therefore, we replace the return value using characters that
*-- are not valid format characters and then replace them at the
*-- end of the program with the appropriate return value...

*-- month
IF "Mmmmm" $ lcRetVal
   lcRetVal = STRTRAN(lcRetVal, "Mmmmm", "Zzzzz", 1, -1) && case-sensitive
ENDIF

IF "MMMMM" $ lcRetVal
   lcRetVal = STRTRAN(lcRetVal, "MMMMM", "ZZZZZ", 1, -1)  && case-sensitive
ENDIF

IF "Mmm" $ lcRetVal
   lcRetVal = STRTRAN(lcRetVal, "Mmm", "Zzz", 1, -1)      && case-sensitive
ENDIF

IF "MMM" $ lcRetVal
   lcRetVal = STRTRAN(lcRetVal, "MMM", "ZZZ", 1, -1)       && case-sensitive
ENDIF

IF "MM" $ UPPER(lcRetVal)
   lcRetVal = STRTRAN(lcRetVal, "MM", lcMM, 1, -1, 1)       && case-insensitive
ENDIF
IF "M" $ UPPER(lcRetVal)
   lcRetVal = STRTRAN(lcRetVal, "M", lcM, 1, -1, 1)         && case-insensitive
ENDIF

*-- Day
IF "Ddddd" $ lcRetVal
   lcRetVal = STRTRAN(lcRetVal, "Ddddd", "Xxxxx", 1, -1) && case-sensitive
ENDIF

IF "DDDDD" $ lcRetVal
   lcRetVal = STRTRAN(lcRetVal, "DDDDD", "XXXXX", 1, -1)  && case-sensitive
ENDIF

IF "Ddd" $ lcRetVal
   lcRetVal = STRTRAN(lcRetVal, "Ddd", "Xxx", 1, -1)      && case-sensitive
ENDIF

IF "DDD" $ lcRetVal
   lcRetVal = STRTRAN(lcRetVal, "DDD", "XXX", 1, -1)       && case-sensitive
ENDIF

IF "DD" $ UPPER(lcRetVal)
   lcRetVal = STRTRAN(lcRetVal, "DD", lcDD, 1, -1, 1)       && case-insensitive
ENDIF
IF "D" $ UPPER(lcRetVal)
   lcRetVal = STRTRAN(lcRetVal, "D", lcD, 1, -1, 1)         && case-insensitive
ENDIF

*-- finally, we replace the special placeholders with the return value

lcRetVal = STRTRAN(lcRetVal, "Zzzzz", lcMmmm_, 1, -1)    && case-sensitive
lcRetVal = STRTRAN(lcRetVal, "ZZZZZ", lcMMMMM, 1, -1)    && case-sensitive
lcRetVal = STRTRAN(lcRetVal, "Zzz", lcMm_, 1, -1)        && case-sensitive
lcRetVal = STRTRAN(lcRetVal, "ZZZ", lcMMM, 1, -1)        && case-sensitive

lcRetVal = STRTRAN(lcRetVal, "Xxxxx", lcDddd_, 1, -1)    && case-sensitive
lcRetVal = STRTRAN(lcRetVal, "XXXXX", lcDDDDD, 1, -1)    && case-sensitive
lcRetVal = STRTRAN(lcRetVal, "Xxx", lcDd_, 1, -1)        && case-sensitive
lcRetVal = STRTRAN(lcRetVal, "XXX", lcDDD, 1, -1)        && case-sensitive

RETURN lcRetVal
*-- EOF cDateTime.prg -----------------------------------------------------------------------------
>Hi all
>
>Does anyone have program that can convert / format dates / datetimes like Excel does?
>
>I know I can ...
>
>
>LPARAMETERS tuDateOrTime
>RETURN ;
> RIGHT("00"+ALLTRIM(STR(MONTH(m.tuDateOrTime))),2)+"/"+;
> RIGHT("00"+ALLTRIM(STR(DAY(m.tuDateOrTime))),2)+"/"+;
> ALLTRIM(STR(YEAR(m.tuDateOrTime)))
>

>
>But something like transform(date(),"MMM-DD-YYYY") to get Feb 10, 2003 seems easier than the current options.
>
>TIA

HTH
Daniel
Previous
Reply
Map
View

Click here to load this message in the networking platform