{ 作用:判断指定日期是星期几。可以从公元1年1月1日算起。 完成时间:0:49 2011-3-9 环境:delphi7+winxp3 测试通过 思路:从公元1年1月1日(为星期一)算起, 将指定日期距离公元第一天的总天数算出来,对7求余, 根据不同余数,指出具体是星期几。 } unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Label1: TLabel; Button1: TButton; Label2: TLabel; Edit2: TEdit; Label3: TLabel; Edit3: TEdit; Label4: TLabel; Label5: TLabel; procedure Button1Click(Sender: TObject); //procedure FormCreate(Sender:TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Year,Month,Day:Integer; implementation {$R *.dfm} //******自定义闰年判断函数***** function IsLeapyear(AYear:Integer):Boolean; begin Result:=((AYear mod 4)=0)and((AYear mod 100)<>0)or((AYear mod 400)=0) end; //******计算该年份的前一年距离公元1年的总天数***** function sumYDay(Y:integer):integer; var nY:integer; YDay:integer; begin YDay:=0; for nY:=1 to Y-1 do begin YDay:=YDay+365; if IsLeapyear(nY) then Inc(YDay);//如果为闰年,自加1。 end; Result:=YDay; end; //*******计算指定月份的前一个月到该年1月1日的总天数****** function sumMDay(M:integer):Integer; var nM,MDay:integer; begin MDay:=0; for nM:=1 to M-1 do begin case nM of 1,3,5,7,8,10,12: MDay:= MDay+31; 2:begin MDay:=Mday+28; if IsLeapyear(Year) then Inc(MDay);//如果该年为闰年,则自加一天 end; else MDay:=MDay+30; end; end; Result:=MDay; end; { procedure TForm1.FormCreate(Sender:TObject); begin Year:=StrToIntDef(Form1.Edit1.Text,0); Month:=StrToIntDef(Form1.Edit2.Text,0); Day:=StrToIntDef(Form1.Edit3.Text,0); end; } procedure TForm1.Button1Click(Sender: TObject); var weekstr:string; week:Integer; ToltalDay:Integer; //从公元1年1日(星期一)算起,到指定日期的总天数 begin Year:=StrToIntDef(Form1.Edit1.Text,0); Month:=StrToIntDef(Form1.Edit2.Text,0); Day:=StrToIntDef(Form1.Edit3.Text,0); ToltalDay:=sumYDay(Year)+sumMDay(Month)+Day; week:=(ToltalDay mod 7); case week of 0:weekstr:='星期日'; 1:weekstr:='星期一'; 2:weekstr:='星期二'; 3:weekstr:='星期三'; 4:weekstr:='星期四'; 5:weekstr:='星期五'; 6:weekstr:='星期六'; end; showmessage('这一天是:'+weekstr); showmessage('距离公元1年1月1日的天数为:'+IntToStr(ToltalDay)+'天 = '+IntToStr(sumYDay(Year))+ ' + '+IntToStr(sumMDay(Month))+' + '+IntToStr(Day)); end; end. 运行结果截图: