Главная страница | назад





Article #17547: TDateTime does not set the field out of 1900

 Question and Answer Database
FAQ2547C.txt TDateTime does not set the field out of 1900
Category :Database Issues
Platform :All
Product :C++Builder 3.x
Question:
I have a Table, assume paradox but the issue is the same with all
tables, and one of the fields is a DATE field. I get to the date
field by using TDataSet.FieldByName ("MyDateField").AsString
I read the string into a AnsiString and then create the TDateTime
with the AnsiString (TDateTime dt (AnsiString)). then I decode
the TDateTime to change the year to something in the 2000 and
then set it back to the dataset using
TDataSet.FieldByName("MyDateField").AsString=dt.DateString();
The field is then not set to the 2000 year but back to the 1900
year. What the heck is the problem. Is TDateTime not year
2000 compliant?
Answer:
No, TDateTime is absolutely year 2000 complient. The problem
is that you are setting the field as a string and something is being
lost in the process. What you need to do is read in the
TDateTime as a TDateTime by using a TDateField or a
TDateTimeField. Instead of this:
AnsiString as = DataSet->FieldByName ("FieldName")->AsString;
TDateTime dt (as, TDateTime::Date);
short int y, m, d;
dt.DecodeDate (&y, &d, &y);
y = 2001;
TDateTime dt2 (y, m, d);
DateSet->Edit ();
DataSet->FieldByName ("FieldName")->AsString = dt2.DateString();
Do this:
TDateField *df = (TDateField*)DataSet->FieldByName ("FieldName");
TDateTime dt = df->AsDateTime;
short int y, m, d;
dt.DecodeDate (&y, &d, &y);
y = 2001;
TDateTime dt2 (y, m, d);
DateSet->Edit ();
df->AsDateTime = dt2;
It will now work fine.
Y.T.
5/13/98 10:40:33 AM

Last Modified: 01-SEP-99