insert number of days of year, months, days of month
How can I insert in a database the number of days of the year and at the same time insert in the same record the month, day of the month, and the day of the week? This is my table:
tabela/coluna.Dias_ano(registo 1...365)
Year:=StrToInt(ano.Text开发者_运维百科);
diasano.Text:= IntToStr( DaysInAYear(Year) );
diasAno| Mes |diames |dia semana | 1 | janeiro | 1 |Segunda | 2 | janeiro | 2 | Terça | ... 365 | Dezembro | 31 | Segunda
Probably I'm missing the question but in case I'm not, you can find what you need in "DateUtils.pas". It has functions like "DayOfTheYear", "MonthOfTheYear", "DayOfTheMonth", "DayOfTheWeek" and many more. I think you're gonna store them in different fields, but there might be a probability that you don't need to store them at all; the database you're using might supply similar functionality, in that case you can construct your queries to supply the filtering/ordering you need.
edit: code for the 3rd comment below;
procedure TForm1.Button1Click(Sender: TObject);
var
Year, DaysInYear: Word;
FirstDay, i: Integer;
begin
Year := StrToInt(ano.Text);
DaysInYear := DaysInAYear(Year);
diasano.Text := IntToStr(DaysInYear);
FirstDay := Trunc(EncodeDate(Year, 1, 1));
for i := FirstDay to FirstDay + DaysInYear - 1 do begin
Planeamento.Append;
Planeamento.FieldByName('diasAno').Value := DayOfTheYear(i);
Planeamento.FieldByName('Month').Value := LongMonthNames[MonthOfTheYear(i)];
Planeamento.FieldByName('DayOfMonth').Value := DayOfTheMonth(i);
Planeamento.FieldByName('DayOfWeek').Value := LongDayNames[DayOfTheWeek(i)];
Planeamento.Post;
end;
end;
edit: With calculated fields;
For the below example the table has five columns instead of four. Let's name the first column 'Date'. This column is the only column to store data and will hold the Date (as per ldsandon's answer, since by storing the date instead of day-number, you won't have to keep track of what table represents what year, and calculations will be simpler). The other four columns are exactly the same as in the question, except that they all are "calculated fields".
procedure TForm1.Button1Click(Sender: TObject);
var
Year, DaysInYear: Word;
FirstDay, i: Integer;
begin
Year := StrToInt(ano.Text);
DaysInYear := DaysInAYear(Year);
diasano.Text := IntToStr(DaysInYear);
FirstDay := Trunc(EncodeDate(Year, 1, 1));
for i := FirstDay to FirstDay + DaysInYear - 1 do begin
Planeamento.Append;
Planeamento.FieldByName('Date').Value := i;
Planeamento.Post;
end;
end;
procedure TForm1.PlaneamentoCalcFields(DataSet: TDataSet);
var
Date: TDateTime;
begin
Date := DataSet.FieldByName('Date').AsDateTime;
DataSet.FieldByName('diasAno').AsInteger := DayOfTheYear(Date);
DataSet.FieldByName('Month').AsString := LongMonthNames[MonthOfTheYear(Date)];
DataSet.FieldByName('DayOfMonth').AsInteger := DayOfTheMonth(Date);
DataSet.FieldByName('DayOfWeek').AsString := LongDayNames[DayOfTheWeek(Date)];
end;
You could simply calculate what value has 1/1/<year>, than check if <year> is leap or not and then with a simple for loop calculate the TDateTime value for each day (just add the day number to the January 1st value), extract the info you need with the DateUtils functions and write them to a record for each day. But I would advise you agains such a solution. Those all are informations already encoded into a datetime value. I would simply store each item with its date, the whole calendar can be easily built client side when needed (and only the needed parts), without having to store it wholly in the database.
精彩评论