Page 1 of 1

Date format in xBrowse

Posted: Mon Jul 07, 2008 9:38 am
by Marco Turco
Hi all,
I use in my app the 8 digit date format dd/mm/yy - yes I have to change it to 4 digit year and I will make it as soon as possible :(

At this moment I would like to use ONLY in a xbrowse (array) the dd/mm/yyyy format. Is there any way to use a specific date format only in the xbrowse without change the date format to all the app ?

Thanks.

Re: Date format in xBrowse

Posted: Mon Jul 07, 2008 10:00 am
by Enrico Maria Giordano

Code: Select all

FUNCTION MAIN()

    LOCAL dDate := DATE()

    ? dDate

    ? STRZERO( DAY( dDate ), 2 ) + "/" + STRZERO( MONTH( dDate ), 2 ) + "/" + LTRIM( STR( YEAR( dDate ) ) )

    RETURN NIL
EMG

Posted: Mon Jul 07, 2008 6:30 pm
by nageswaragunupudi
I personally use my function "dtocfmt( <date>, <cformat> )", whenever I need to show date in a non-conventional format or a format different than the Set Date format.

In a case like this I would

bStrDate := { || dtocfmt( <datevar>, 'dd-mm-yyyy' ) }

Here is the source code of the function:

Code: Select all

function dtocfmt( dDate, cFormat )

   local cDate

   DEFAULT cFormat := Set( _SET_DATEFORMAT )   

   cDate := Lower( cFormat )

   cDate    := StrTran( cDate, 'dd', StrZero( Day( dDate ), 2 ) )
   if 'mmmm' $ cDate
      cDate    := StrTran( cDate, 'mmmm', cMonth( dDate ) )
   elseif 'mmm' $ cDate
      cDate    := StrTran( cDate, 'mmm', Left( cMonth( dDate ), 3 ) )
   else
      cDate    := StrTran( cDate, 'mm', StrZero( Month( dDate ), 2 ) )
   endif
   if 'yyyy' $ cDate
      cDate    := StrTran( cDate, 'yyyy', Str( Year( dDate ), 4, 0 ) )
   else
      cDate    := StrTran( cDate, 'yy',   StrZero( Year( dDate ) % 100, 2 ) )
   endif

return cDate
Format is case insenstive and date, month and year can be positioned anywhere.

Example formats and results:

dd-mm-yy ->22-07-08
dd-mmm-yyyy -> 22-Oct-2008
mmmm dd, yyyy -> October 02, 2008
mmm yyyy -> OCT 2008

Posted: Tue Jul 08, 2008 6:34 am
by Detlef Hoefner
nageswaragunupudi,

a clever coded function.
Thanks for sharing it.

Regards,
Detlef

Posted: Tue Jul 08, 2008 3:08 pm
by Marco Turco
Hi all,
thanks for the tips
but how can I use a specific date format on a xbrowse ?

This is a sample code that show the problem:

#include "FiveWin.ch"
#include "xbrowse.ch"

function Main()

local oDlg, oBrw, aArray

set century off && I can't change this

aArray:={}
aadd(aArray,{"First",ctod("01/01/2008")})
aadd(aArray,{"Second",ctod("01/04/2008")})
aadd(aArray,{"Thirth",ctod("01/06/2008")})

DEFINE DIALOG oDlg SIZE 300, 200

@ 0, 0 XBROWSE oBrw OF oDlg ARRAY aArray AUTOCOLS

oBrw:CreateFromCode()

ACTIVATE DIALOG oDlg CENTER

return nil


If you execute this code you will see that the dates in the browse appair in the format dd/mm/yy due the century off setting.

I would like to display the dates in the browse in the format dd/mm/yyyy without change the century off setting.
Note that I need to leave a date format for the dates in the array in order to permit the customer to change the date order with a click on the header.

Posted: Tue Jul 08, 2008 4:48 pm
by nageswaragunupudi
Please insert this line before oBrw:CreateFromCode()

Code: Select all

oBrw:aCols[ 2 ]:bStrData := { || dtocfmt( oBrw:aRow[ 2 ], 'dd/mm/yyyy' ) }
Include the source code of funtion dtocfmt I posted earlier either in the same module or in your libray

Posted: Tue Jul 08, 2008 9:25 pm
by James Bott
Marco,
Note that I need to leave a date format for the dates in the array in order to permit the customer to change the date order with a click on the header.
In order to do this you will need to use DTOS() otherwise dates in different years won't sort correctly. Since you don't want to display the dates in DTOS() format, you may have to either sort the array DTOS() then redisplay the browse when the user clicks the header, or create a temp dbf to hold the array, then create an index using DTOS(). With the dbf you can index on one format and display another.

Since your dates are in character format, you will actually have to do DTOS( CTOD( cDate ) ).

James

Posted: Wed Jul 09, 2008 4:16 am
by nageswaragunupudi
You may try this example.

Dates are stored in the array as date type only. Dates are sorted properly when the user clicks on the header because dates are stored in the date type only.

Dates can be showin in xbrowse in any format independant of the SetDate format. Also in this example, date format can be changed on the fly at any time. ( Right click on the date column pops up a menu to choose the date format and whatever format is chosen, dates are sorted properly )


Code: Select all

#include 'fivewin.ch'
#include 'xbrowse.ch'

static cFormat  := 'dd/mm/yyyy'

//----------------------------------------------------------------------------//

function Main()

   local oDlg, oBrw, aArray

   SET CENTURY OFF

   aArray:={}
   AAdd( aArray, { "First  ", SToD("20080101") } )
   AAdd( aArray, { "Second ", SToD("20080401") } )
   AAdd( aArray, { "Third  ", SToD("20080601") } )

   DEFINE DIALOG oDlg SIZE 300, 200

   @ 0, 0 XBROWSE oBrw OF oDlg ;
      HEADERS 'Detail','Date' ;
      SIZE 150,100 PIXEL  ;
      ARRAY aArray AUTOCOLS AUTOSORT

   oBrw:aCols[ 2 ]:bStrData  := { ||  dtocfmt( oBrw:aRow[ 2 ], cFormat ) }
   oBrw:aCols[ 2 ]:bPopUp    := { |o| ChooseDateFormat( o ) }
   oBrw:nStretchCol  := 2

   oBrw:CreateFromCode()

   ACTIVATE DIALOG oDlg CENTER

return nil

//----------------------------------------------------------------------------//

static function ChooseDateFormat( oCol )

   local oPop

   MENU oPop POPUP
      MENUITEM 'dd/mm/yyyy'    ACTION ( cFormat := oMenuItem:cPrompt, oCol:oBrw:Refresh() )
      MENUITEM 'dd/mm/yy'      ACTION ( cFormat := oMenuItem:cPrompt, oCol:oBrw:Refresh() )
      MENUITEM 'mm/dd/yyyy'    ACTION ( cFormat := oMenuItem:cPrompt, oCol:oBrw:Refresh() )
      MENUITEM 'dd mmm yyyy'   ACTION ( cFormat := oMenuItem:cPrompt, oCol:oBrw:Refresh() )
      MENUITEM 'mmmm dd, yyyy' ACTION ( cFormat := oMenuItem:cPrompt, oCol:oBrw:Refresh() )
   ENDMENU

return oPop

//----------------------------------------------------------------------------//

function dtocfmt( dDate, cFormat )

   local cDate

   DEFAULT cFormat := Set( _SET_DATEFORMAT )

   cDate := Lower( cFormat )

   cDate    := StrTran( cDate, 'dd', StrZero( Day( dDate ), 2 ) )
   if 'mmmm' $ cDate
      cDate    := StrTran( cDate, 'mmmm', cMonth( dDate ) )
   elseif 'mmm' $ cDate
      cDate    := StrTran( cDate, 'mmm', Left( cMonth( dDate ), 3 ) )
   else
      cDate    := StrTran( cDate, 'mm', StrZero( Month( dDate ), 2 ) )
   endif
   if 'yyyy' $ cDate
      cDate    := StrTran( cDate, 'yyyy', Str( Year( dDate ), 4, 0 ) )
   else
      cDate    := StrTran( cDate, 'yy',   StrZero( Year( dDate ) % 100, 2 ) )
   endif

return cDate

//----------------------------------------------------------------------------//

Screenshots:

Image

Image

Image

Posted: Wed Jul 09, 2008 10:50 am
by Marco Turco
Ok. Solved.

Thanks all for the support.