tags:

views:

142

answers:

2

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 
+2  A: 

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;
Sertac Akyuz
i have to use something like the code i say because it wil be like a work calendar for the all year working days . so i will give the Year and i have to insert de nº of days in the year (2010= 365 days) where day 1 wil be january,1,tuesday and so on..in each record (365 record´s) then i only nead to inseart task´s amd filter by today (DaysInAYear(Year) but how can i insert wall at once!!Thank´s
ml
You could loop through "day no"s while calculating "day of" values and inserting them to a table for each iteration. But I suppose this wouldn't be inserting "all at once".... Still, if all you need that information is on the client side, you don't need to store them. You can present them by means of calculated fields. All you need is a one to many relationship from a "day-no" table to a "tasks" table.
Sertac Akyuz
what i what to do once in the year is something like below :procedure TPlaneamento.Button1Click(Sender: TObject);Varijkmbegin inherited; Year:=StrToInt(ano.Text); diasano.Text:= IntToStr( DaysInAYear(Year) );m:=(FormatDateTime('dddd',Now)) for I := 1 to StrToInt(diasano.Text) do begin frmDados.Planeamento.Append; frmDados.Planeamento.FieldByName('month').value:=I; frmDados.Planeamento.FieldByName('dayofmonth').value:=I;frmDados.Planeamento.FieldByName('dayofmonth').value:=I;frmDados.Planeamento.FieldByName('dayofweek').value:=j;Thank´s
ml
@ml - Updated the answer.
Sertac Akyuz
+1  A: 

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.

ldsandon