ChrisR has asked for the wisdom of the Perl Monks concerning the following question:

I've been using Win32::Grid control and setting the CellType to GVIT_DATECAL. Everything works great except that I can't figure out how to set the format of the date control. I need the date produced by the control to be in the format "yyyy-MM-dd" but the control seems to default to the short-date format of the system which is "M-d-yyyy" in my case. I have tried the ->SetCellFormat("yyyy-MM-dd") method in Grid and it doesn't error but it doesn't work either. I tried to use the DateTime method ->Format("yyyy-MM-dd") which errors out. I have searched google, sourceforge, and activestate for this but can't find anything. I looked at Grid.pm and couldn't find any way to change the default format in there. Can anyone give a me hint of how to do this??

Thanks, Chris

Replies are listed 'Best First'.
Re: Win32::GUI::Grid & Date Formats
by anonymized user 468275 (Curate) on Jul 20, 2005 at 10:56 UTC
    Reformatting it doesn't seem that difficult. Here's a function that would do it:
    sub Reformat { my @field = split( '-', shift()); my $result = $field[2]; for my $idx (0..1) { ( $field[ $idx ] < 10 ) and $field[ $idx ] = '0' . $field[ $idx ]; $result .= ( '-' . $field[ $idx ] ); } return $result; }

    One world, one people

      Perhaps I need to do a little more clarification here. I do not need to format a date string. I need to et the control so that it formats the date string. The Win32::GUI::DateTime control, which is what I suspect Grid is using, can be set to use different dat/time format via the Format method. I just don't know how to access the DateTime object within the Grid Cell object so that it displays the correct format. Here is a short piece of code to demonstrate the issue:
      #!c:\perl\wperl.exe use strict; use Win32::GUI; use Win32::GUI::Grid; my $mainform = Win32::GUI::Window->new(-name=>'main',-text=>'Grid Date + Format Problem',-width=>810,-height=>625,-dialogui=>1); my $grid = $mainform->AddGrid(-name=>'grid',-pos=>[25,85],-rows=>2,-co +lumns=>3,-fixedrows=>1,-fixedcolumns=>1,-editable=>1,-size=> [520,400 +],-addstyle=>WS_VSCROLL|WS_TABSTOP); $grid->SetColumnWidth(0,100); $grid->SetColumnWidth(1,100); $grid->SetColumnWidth(2,100); $grid->SetRowHeight(1,30); $grid->SetCellText(0, 0, "ID"); $grid->SetCellText(0, 1, "GVIT_DATECAL"); $grid->SetCellText(0, 2, "GVIT_DATE"); $grid->SetCellFormat(1, 1, 'yyyy-MM-dd'); $grid->SetCellType(1, 1, GVIT_DATECAL); $grid->SetCellText(1, 1, "2005-07-19"); $grid->SetCellFormat(1, 2, 'yyyy-MM-dd'); $grid->SetCellType(1, 2, GVIT_DATE); $grid->SetCellText(1, 2, "2005-07-19"); $mainform->Show(); Win32::GUI::Dialog();
      The SetCellFormat method is probably not what I need to use here but I have no idea how to make it work. After you select a date using the control, the cell will display the date in whatever your short date format is. If you change your short date format and re-run the program, it will display in the new format you set. I want to ignore the default setting for the system and set the format myself.

      By the way, the code you provided doesn't work as far as I can tell. Whether I send it a date like 2005/7/19 or 7/19/2005 it returns something that doesn't look like a date to me.

      But again, thanks for replying.

      Chris
        I was in the process of thinking that your code was experiencing a date format problem between a db and the GUI. But I don't think this is the case. Looking at the screen everything displays correctly until you click in the cell that contains the date and then it changes to the current date in a different format. That is a little annoying. Is this the problem that you are talking about. Or, is it a db GUI format problem?

        Doug

        Not wanting to get too deep into the GUI at this stage, my first thought now is to set the type in the GUI to string and use the Date::Format module to do such general formatting and then repost the formatted string with the GUI methods.

        One world, one people

          A reply falls below the community's threshold of quality. You may see it by logging in.