Date format in xBrowse

Date format in xBrowse

Postby Marco Turco » Mon Jul 07, 2008 9:38 am

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.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Date format in xBrowse

Postby Enrico Maria Giordano » Mon Jul 07, 2008 10:00 am

Code: Select all  Expand view  RUN
FUNCTION MAIN()

    LOCAL dDate := DATE()

    ? dDate

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

    RETURN NIL


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8716
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby nageswaragunupudi » Mon Jul 07, 2008 6:30 pm

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  Expand view  RUN
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
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Postby Detlef Hoefner » Tue Jul 08, 2008 6:34 am

nageswaragunupudi,

a clever coded function.
Thanks for sharing it.

Regards,
Detlef
User avatar
Detlef Hoefner
 
Posts: 312
Joined: Sat Oct 08, 2005 9:12 am
Location: Germany

Postby Marco Turco » Tue Jul 08, 2008 3:08 pm

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.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Postby nageswaragunupudi » Tue Jul 08, 2008 4:48 pm

Please insert this line before oBrw:CreateFromCode()
Code: Select all  Expand view  RUN
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
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Postby James Bott » Tue Jul 08, 2008 9:25 pm

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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby nageswaragunupudi » Wed Jul 09, 2008 4:16 am

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  Expand view  RUN
#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
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Postby Marco Turco » Wed Jul 09, 2008 10:50 am

Ok. Solved.

Thanks all for the support.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: nageswaragunupudi and 100 guests