Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Reverste date

Dear Experts,

I have a dataset with data on my form created the following way:
->a DBGrid1 on myform with property DataSource set to DataSource1.
->a DataSource1 with property DataSet set to ClientDataset1
->a ClientDataSet with property ProviderName set to XMLTransformProvider1.
    and property Active to True.
->a XMLTransformProvider1 with the property FileName set to a xml-filename.
->and a DBNavigator with the propery DataSource set to DataSource1.
->and I have set the 3 columns:'dt','tm' and 'val' (dt=date,tm=time,and val=value)
    as persistend fields in the DBGrid.

The date in the xml-file is: yy-mm-dd
and I would like to have it displayed backwords in the dbgrid: dd-mm-yy

I have allready these procedures:

function CustomStrToDate(DateString : string) : TDatetime;
var
  dd, mm, yy : string;
begin
 result := 0;
  yy := copy(DateString,1, 4);
  mm := copy(DateString,6, 2);
  dd := copy(DateString,9, 2);
  result := encodedate(strtoint(yy), strtoint(mm), strtoint(dd));
end;
(*---------------------------------------------------*)
procedure TMainForm.ClientDataSet1CalcFields(DataSet: TDataSet);
const
  Days : array[0..6] of string
    = ('Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag');
const
  DoubleFormat: TFormatSettings = (DecimalSeparator: '.');
var
  Str: string;
  Value: Extended;
begin
  clientdataset1.fieldbyname('Da').asstring := Days[DayOfTheWeek(CustomStrToDate(clientdataset1.fieldbyname('Dt').asstring))-1];
  Str:= StringReplace(ClientDataSet1.FieldByName('Val').AsString, ',', '.', []);
  if TryStrToFloat(Str, Value, DoubleFormat) then
  begin
    if Value < 4 then
      ClientDataSet1.FieldByName('St').AsString:= 'HYPO'
    else if Value > 10 then
      ClientDataSet1.FieldByName('St').AsString:= 'HYPER'
    else
      ClientDataSet1.FieldByName('St').AsString:= '';
  end
  else
      ClientDataSet1.FieldByName('St').AsString:= '';
end;

Who knows the anser how to reverse the date.
Peter

Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

You can add a new calculated field called rdt

      ClientDataSet1.FieldByName('rdt').AsString:= copy(DateString,9, 2) +'-' +copy(DateString,6, 2) +'-' + copy(DateString,1, 4);

And add that persistent field to the Grid
Avatar of Peter Kiers

ASKER

Could you please be more specific or guide me step by step.

P.
you need a query to reverse the sorting

DBGrid doesn't natively support sorting on the grid itself
you need to sort the data

or using a other dbgrid like TCRDBGrid from DevArt

don't you have a grid in the jedi components ?
that should allow for sorting
ow, you only want the date in a other format in the column ?

add a calculated field of type string
ClientDataSet1.FieldByName('datecolumn').AsString:= FormatDateTime('dd-mm-yy', clientdataset1.fieldbyname('Da').asDateTime);



ASKER CERTIFIED SOLUTION
Avatar of aflarin
aflarin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ClientDataSet1.FieldByName('Dtr').AsString:= FormatDateTime('dd-mm-yy', clientdataset1.fieldbyname('Dt').asDateTime);

error: 2010-06-19 is not a valid date and time

P
Aflarin came with the right solution it works great.
Thanks.

Peter Kiers
my solution is correct providing you saved the data as a datetime and not as a string in your database
otherwise you should actually accept cyberkiwi's solution

because it is not a date conversion but a string to string conversion
allways try to store data in a non converted way in the database

if you want to see 2 decimals on the screen and you have 6 decimals in your value
you will store 6 decimals in your db and not 2
you will keep getting errors otherwise

keep in mind that your application will only work in your own country
or with your country settings

if somebody else has different country settings for the dates, your app will not work