unit Forma;interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForma1 = class(TForm) Edit1: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; GetWeekBtn: TButton; Label4: TLabel; procedure GetWeekBtnClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } Function HowManyDays(pYear,pMonth,pDay:word):integer; public { Public declarations } end; var Forma1: TForma1;implementation{$R *.DFM} Uses Inifiles;procedure TForma1.FormCreate(Sender: TObject); var WinIni:TInifile; begin WinIni:=TIniFile.Create('WIN.INI'); WinIni.WriteString('intl','sShortDate','MM/dd/yyyy'); WinIni.Free; end;Function TForma1.HowManyDays(pYear,pMonth,pDay:word):integer; var Sum:integer; pYearAux:word; begin Sum:=0; if pMonth>1 then Sum:=Sum+31; if pMonth>2 then Sum:=Sum+28; if pMonth>3 then Sum:=Sum+31; if pMonth>4 then Sum:=Sum+30; if pMonth>5 then Sum:=Sum+31; if pMonth>6 then Sum:=Sum+30; if pMonth>7 then Sum:=Sum+31; if pMonth>8 then Sum:=Sum+31; if pMonth>9 then Sum:=Sum+30; if pMonth>10 then Sum:=Sum+31; if pMonth>11 then Sum:=Sum+30; Sum:=Sum + pDay; if ((pYear - (pYear div 4)*4)=3D0) and (pMonth>2)then inc(Sum); HowManyDays:=Sum; end; { HowManyDays }procedure TForma1.GetWeekBtnClick(Sender: TObject); var ADate: TDateTime; EditAux:String; Week,year,month,day:Word; begin EditAux:=Edit1.Text; ADate := StrToDate(EditAux); Label1.Caption := DateToStr(ADate); DecodeDate(Adate,Year,Month,Day); Case DayOfWeek(ADate) of 1: Label4.Caption:='Sunday'; 2: Label4.Caption:='Monday'; 3: Label4.Caption:='Tuesday'; 4: Label4.Caption:='Wednesday'; 5: Label4.Caption:='Thursday'; 6: Label4.Caption:='Friday'; 7: Label4.Caption:='Saturday'; end Week:=(HowManyDays(year,month,day) div 7) +1; Label3.Caption:='Week No. '+IntToStr(Week); end;end.
首部 function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean; $[SysUtils.pas 功能 分解日期为年、月、日、星期 说明 [DOW:Day Of Week] 参考 function SysUtils.DateTimeToTimeStamp 例子 ///////Begin DecodeDateFully procedure TForm1.Button1Click(Sender: TObject); var Year, Month, Day, DOW: Word; begin DecodeDateFully(Date, Year, Month, Day, DOW); SpinEdit1.Value := Year; SpinEdit2.Value := Month; SpinEdit3.Value := Day; SpinEdit4.Value := DOW; end; ///////End DecodeDateFully
首部 function DayOfWeek(const DateTime: TDateTime): Word; $[SysUtils.pas 功能 返回日期时间DateTime所在的星期 说明 1(星期天),2(星期一),3(星期二),4(星期三),5(星期四),6(星期五),7(星期六) 参考 function SysUtils.DateTimeToTimeStamp 例子 ///////Begin DayOfWeek procedure TForm1.Button1Click(Sender: TObject); const cWeekCn: array[1..7] of string = ('星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'); begin Edit1.Text := cWeekCn[DayOfWeek(Now)]; end; ///////End DayOfWeek
大致的可以这样,你可以参照一下,我没怎么测试,你看看吧:procedure TForm1.Button1Click(Sender: TObject); type Ta = array[1..14] of TDateTime; var a: Ta; i:integer; begin for i:=1 to 14 do begin a[i] := IncDay(StrToDate('2003-10-02'),-(7+DayOfTheWeeK(StrToDate('2003-10-02')) - i)); end; end;
procedure TForm1.Button1Click(Sender: TObject); var nTotalPrv,nTotalNxt,nWeek:Word; ArrDate:Array[1..14] of Tdatetime; begin nWeek:=DayOfWeek(StrToDate('2003-10-02')); Edit2.Text:=IntToStr(nWeek); nTotalPrv:=7+nWeek; nTotalNxt:=7-nWeek; for nWeek:=0 to nTotalPrv-1 do//本日前的所有日期 ArrDate[nWeek+1]:=StrToDate('2003-10-02')-nWeek; for nWeek:=1 to nTotalNxt do //本日后的所有日期 ArrDate[nTotalPrv+nWeek]:=StrToDate('2003-10-02')+nWeek; ListBox1.Items.Clear; for nWeek:=Low(ArrDate) to High(ArrDate) do ListBox1.Items.Add(DateTostr(ArrDate[nWeek])); end;
DayOfTheWeek得到所在的这个星期几!有这两个应该写起来,没困难了吧?
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;type
TForma1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
GetWeekBtn: TButton;
Label4: TLabel;
procedure GetWeekBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
Function HowManyDays(pYear,pMonth,pDay:word):integer;
public
{ Public declarations }
end;
var
Forma1: TForma1;implementation{$R *.DFM}
Uses Inifiles;procedure TForma1.FormCreate(Sender: TObject);
var WinIni:TInifile;
begin
WinIni:=TIniFile.Create('WIN.INI');
WinIni.WriteString('intl','sShortDate','MM/dd/yyyy');
WinIni.Free;
end;Function TForma1.HowManyDays(pYear,pMonth,pDay:word):integer;
var Sum:integer;
pYearAux:word;
begin
Sum:=0;
if pMonth>1 then Sum:=Sum+31;
if pMonth>2 then Sum:=Sum+28;
if pMonth>3 then Sum:=Sum+31;
if pMonth>4 then Sum:=Sum+30;
if pMonth>5 then Sum:=Sum+31;
if pMonth>6 then Sum:=Sum+30;
if pMonth>7 then Sum:=Sum+31;
if pMonth>8 then Sum:=Sum+31;
if pMonth>9 then Sum:=Sum+30;
if pMonth>10 then Sum:=Sum+31;
if pMonth>11 then Sum:=Sum+30; Sum:=Sum + pDay;
if ((pYear - (pYear div 4)*4)=3D0) and
(pMonth>2)then
inc(Sum); HowManyDays:=Sum;
end; { HowManyDays }procedure TForma1.GetWeekBtnClick(Sender: TObject);
var
ADate: TDateTime;
EditAux:String;
Week,year,month,day:Word;
begin
EditAux:=Edit1.Text;
ADate := StrToDate(EditAux);
Label1.Caption := DateToStr(ADate);
DecodeDate(Adate,Year,Month,Day); Case DayOfWeek(ADate) of
1: Label4.Caption:='Sunday';
2: Label4.Caption:='Monday';
3: Label4.Caption:='Tuesday';
4: Label4.Caption:='Wednesday';
5: Label4.Caption:='Thursday';
6: Label4.Caption:='Friday';
7: Label4.Caption:='Saturday';
end
Week:=(HowManyDays(year,month,day) div 7) +1; Label3.Caption:='Week No. '+IntToStr(Week);
end;end.
功能 分解日期为年、月、日、星期
说明 [DOW:Day Of Week]
参考 function SysUtils.DateTimeToTimeStamp
例子
///////Begin DecodeDateFully
procedure TForm1.Button1Click(Sender: TObject);
var
Year, Month, Day, DOW: Word;
begin
DecodeDateFully(Date, Year, Month, Day, DOW);
SpinEdit1.Value := Year;
SpinEdit2.Value := Month;
SpinEdit3.Value := Day;
SpinEdit4.Value := DOW;
end;
///////End DecodeDateFully
功能 返回日期时间DateTime所在的星期
说明 1(星期天),2(星期一),3(星期二),4(星期三),5(星期四),6(星期五),7(星期六)
参考 function SysUtils.DateTimeToTimeStamp
例子
///////Begin DayOfWeek
procedure TForm1.Button1Click(Sender: TObject);
const
cWeekCn: array[1..7] of string =
('星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
begin
Edit1.Text := cWeekCn[DayOfWeek(Now)];
end;
///////End DayOfWeek
type Ta = array[1..14] of TDateTime;
var
a: Ta;
i:integer;
begin
for i:=1 to 14 do
begin
a[i] := IncDay(StrToDate('2003-10-02'),-(7+DayOfTheWeeK(StrToDate('2003-10-02')) - i)); end;
end;
a[i] := IncDay(StrToDate('2003-10-02'),-(7+DayOfTheWeeK(StrToDate('2003-10-02')) - i+1));
var
nTotalPrv,nTotalNxt,nWeek:Word;
ArrDate:Array[1..14] of Tdatetime;
begin
nWeek:=DayOfWeek(StrToDate('2003-10-02'));
Edit2.Text:=IntToStr(nWeek);
nTotalPrv:=7+nWeek;
nTotalNxt:=7-nWeek;
for nWeek:=0 to nTotalPrv-1 do//本日前的所有日期
ArrDate[nWeek+1]:=StrToDate('2003-10-02')-nWeek; for nWeek:=1 to nTotalNxt do //本日后的所有日期
ArrDate[nTotalPrv+nWeek]:=StrToDate('2003-10-02')+nWeek; ListBox1.Items.Clear;
for nWeek:=Low(ArrDate) to High(ArrDate) do
ListBox1.Items.Add(DateTostr(ArrDate[nWeek]));
end;
哈我的比你的简单些吧!^_^
liufuyaho的方法比我的好吗?