我以前也做过一个定时发送信息的程序,用户要求如下:
1、可以以月为周期设定每月那一天几点发送。
2、可以以周为周期设定每周的星期几点发送。
3、可以在指定的日期时间进行发送。
4、对于1和2可以设定发送的次数、可以指定开始发送日期和结束发送日期。
我想知到的是用你这个控件可以实现吗?
============================================================
我给自己做了一个事件提醒软件,设置一个闹铃的时候可以选择类型比如
1、每天型(比如要休息啦)
2、每周型(比如该交每周工作报告)
3、每年型(亲人的生日)
还有时、月等等。闹铃会周期的提醒,除非你关闭这个闹铃。这部分我没有做在这个控件里,而是用函数来实现。
function GetTimeAsType(DT : TDateTime;Str: String): TDateTime;
用这个函数可以返回该周期的下一个提醒。结合上面的闹铃控件,就可以实现你所说的功能。
下面是该单元文件:
unit JessTimeUtils;
interface
Uses
DateUtils,SysUtils,Controls,Dialogs;
function NextWeek(Value: TDateTime) : TDateTime;
function NextDay(Value: TDateTime) : TDateTime;
function NextMinute(Value : TDateTime): TDateTime;
function NextHour(Value : TDateTime): TDateTime;
function NextMonth(Value : TDateTime): TDateTime;
function NextYear(Value : TDateTime): TDateTime;
function GetTimeAsType(DT : TDateTime;Str: String): TdateTime;
implementation
function GetTimeAsType(DT : TDateTime;Str: String): TDateTime;
begin
if AnsicompareText(Str,'minute') = 0 then
begin
Result := NextMinute(DT);
end
else if AnsicompareText(Str,'hour') = 0 then
begin
Result := NextHour(DT);
end
else if AnsicompareText(Str,'day') = 0 then
begin
Result := NextDay(DT);
end
else if AnsicompareText(Str,'week') = 0 then
begin
Result := NextWeek(DT);
end
else if AnsicompareText(Str,'month') = 0 then
begin
Result := NextMonth(DT);
end
else if AnsicompareText(Str,'year') = 0 then
begin
Result := NextYear(DT);
end
else
begin
Result := DT;
end;
end;
function NextMonth(Value : TDateTime): TDateTime;
Var
Year,Month,Day,Hour,Minute,Second,mSecond : Word;
Day_Check : Word;
Time_Check : TTime;
Time_Now : TTime;
Date_Now : TDate;
DT_Check : TDateTime;
begin
{
DecodeDateTime(Value,Year,Month,Day,Hour,Minute,Second,mSecond);
Time_Check := EncodeTime(Hour,Minute,Second,mSecond);
Time_Now := now();
Date_Now := now();
}
Time_Now := Now;
DecodeDateTime(Value,Year,Month,Day,Hour,Minute,Second,mSecond);
Day_Check := Day;
DecodeDate(Time_Now,Year,Month,Day);
DT_Check := EncodeDateTime(Year,Month,Day_Check,Hour,Minute,Second,mSecond);
if DT_Check > Time_Now then
begin
Result := DT_Check;
end
Else
begin
Result := IncMonth(DT_Check,1);
end;
end;
function NextYear(Value : TDateTime): TDateTime;
Var
Year,Month,Day,Hour,Minute,Second,mSecond : Word;
Year_Now : Word;
DT_Now : TDateTime;
DT_Check : TDateTime;
begin
DT_Now := Now;
DecodeDate(DT_Now,Year,Month,Day);
Year_Now := Year;
DecodeDateTime(Value,Year,Month,Day,Hour,Minute,Second,mSecond);
DT_Check := EncodeDateTime(Year_Now,Month,Day,Hour,Minute,Second,mSecond);
if DT_Check > DT_Now then
begin
Result := DT_Check;
end
Else
begin
Result := IncYear(DT_Check,1);
end;
end;
function NextMinute(Value : TDateTime): TDateTime;
Var
Year,Month,Day,Hour,Minute,Second,mSecond : Word;
Second_Check,mSecond_Check : Word;
DT_Check : TDateTime;
DT_Now : TDateTime;
begin
DecodeTime(Value,Hour,Minute,Second,mSecond);
Second_Check := Second;mSecond_Check := mSecond;
DT_Now := Now;
DecodeDateTime(DT_Now,Year,Month,Day,Hour,Minute,Second,mSecond);
DT_Check := EncodeDateTime(Year,Month,Day,Hour,Minute,Second_Check,mSecond_Check);
if DT_Check > DT_Now then
begin
Result := DT_Check;
end
Else
begin
Result := IncMinute(DT_Check,1);
end;
{
DecodeTime(Value,Hour,Minute,Second,mSecond);
Sec_Check := Second;
mSec_Check := mSecond;
DecodeDateTime(Now,Year,Month,Day,Hour,Minute,Second,mSecond);
Time_Check := EncodeTime(Hour,Minute,Sec_Check,mSec_check);
Time_Now := now();
DT_Check := EncodeDateTime(Year,Month,Day,Hour,Minute,Sec_Check,mSec_Check);
if Time_Check <= Time_Now then
begin
Result := IncMinute(DT_Check,1);
exit;
end;
}
end;
function NextDay(Value: TDateTime) : TDateTime;
Var
Year,Month,Day,Hour,Minute,Second,mSecond : Word;
Time_Check : TTime;
Time_Now : TTime;
Date_Now : Tdate;
DT_Check : TDateTime;
begin
Time_Now := Now;
DecodeTime(Value,Hour,Minute,Second,mSecond);
DecodeDate(Time_Now,Year,Month,Day);
DT_Check := EncodeDateTime(Year,Month,Day,Hour,Minute,Second,mSecond);
if DT_Check > Time_Now then
begin
Result := DT_Check;
end
Else
begin
Result := IncDay(DT_Check,1);
end;
end;
function NextHour(Value: TDateTime) : TDateTime;
Var
Year,Month,Day,Hour,Minute,Second,mSecond : Word;
Hour_Now : word;
DT_Check : TDateTime;
DT_Now : TDateTime;
begin
DT_Now := Now;
DecodeTime(DT_Now,Hour,Minute,Second,mSecond);
Hour_Now := Hour;
DecodeTime(Value,Hour,Minute,Second,mSecond);
DecodeDate(DT_Now,Year,Month,Day);
DT_Check := EncodeDateTime(Year,Month,Day,Hour_Now,Minute,Second,mSecond);
if DT_Check > DT_Now then
begin
Result := DT_Check;
end
Else
begin
Result := IncHour(DT_Check,1);
end;
end;
{---------------------------------------}
//-Value: 2006-8-12 18:00:00 Sat
//-Now : 2006-8-12 18:00:00 Sat
//-Result: 2006-8-19 18:00:00 Sat
function NextWeek(Value: TDateTime) : TDateTime;
Var
Week : word;
Week_Now : word;
Time_Check: TTime;
Time_Now : TTime;
Date_Now : TDate;
Year,Month,Day,Hour,Minute,Second,mSecond : Word;
begin
DecodeDateTime(Value,Year,Month,Day,Hour,Minute,Second,mSecond);
Time_Check := EncodeTime(Hour,Minute,Second,mSecond);
Time_Now := now();
Date_Now := now();
Week := DayofWeek(Value);
Week_Now := DayOfWeek(Now);
if Week = Week_Now then
begin
if Time_Check <= Time_Now then
Date_Now := IncDay(Date_Now,7);
end
else if Week < Week_Now then
begin
Date_Now := IncDay(Date_Now,7-(Week_Now-Week));
end
else
begin
Date_Now := IncDay(Date_Now,Week-Week_Now);
end;
DecodeDate(Date_Now,Year,Month,Day);
Result := EncodeDateTime(Year,Month,Day,Hour,Minute,Second,mSecond);
end;
end.