编程偶得:一组日期处理函数

da_yu 2003-10-09 11:20:03



//日期3=日期2-日期1(格式:YYYY-MM-DD)
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit3.text :=inttostr(trunc(StrToDate(edit2.text) - StrToDate(edit1.text)));
end;

//日期4=日期1加1(格式:YYYY-MM-DD)
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit4.text :=DateToStr(Succ(trunc(StrToDate(edit1.text))));
end;

//日期5=日期1格式化(变成YYYYMMDD)
procedure TForm1.Button3Click(Sender: TObject);
begin
Edit5.text :=FormatDateTime('yyyymmdd',StrToDate(edit1.text));
end;

//日期6=日期4格式化(变成YYYYMMDD)
procedure TForm1.Button4Click(Sender: TObject);
begin
Edit6.text :=FormatDateTime('yyyymmdd',StrToDate(edit4.text));
end;


//日期递增(格式:YYYY-MM-DD)
procedure TForm1.Button5Click(Sender: TObject);
begin
Edit7.text :=DateToStr(Succ(trunc(StrToDate(edit7.text))));
end;

//格式化递增
procedure TForm1.Button6Click(Sender: TObject);
begin
Edit8.text :=FormatDateTime('yyyy-mm-dd',(Succ(trunc(StrToDate(edit8.text)))));
Edit9.text :=FormatDateTime('yyyymmdd',StrToDate(edit8.text));
end;
...全文
33 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
LVOLCANO 2003-10-09
  • 打赏
  • 举报
回复
Up
FrameSniper 2003-10-09
  • 打赏
  • 举报
回复
顶!
fyt234 2003-10-09
  • 打赏
  • 举报
回复
好,顶!!!
根据月份,判断!
Case语句就搞定了!
Iamfish 2003-10-09
  • 打赏
  • 举报
回复
有没有周方面的?

周报表有人做过吗?
jianxin_js 2003-10-09
  • 打赏
  • 举报
回复
收藏,顺便问一下,怎么获得日期所属于的“季度”?
dickeybird888 2003-10-09
  • 打赏
  • 举报
回复
哈哈!
dickeybird888 2003-10-09
  • 打赏
  • 举报
回复
来我给你加点:
///////////////////////////////////////////////////////////////////////////
// 功能:获得某日期所在月份的第一天 //
// 入口参数:TDateTime 某日期 //
// 返回值: 某日期所在月份的第一天 //
///////////////////////////////////////////////////////////////////////////
function MonthBegin(Date:TDateTime):TDateTime;
var
T:String;
var
Year, Month, Day{, Hour, Min, Sec, MSec}: Word;
begin
DecodeDate(Date, Year, Month, Day);
T:=IntToStr(Year)+'-'+IntToStr(Month)+'-';
Result:=StrToDate(T+'1')
end;

///////////////////////////////////////////////////////////////////////////
// 功能:获得某日期所在月份的最后一天 //
// 入口参数:TDateTime 某日期 //
// 返回值: 某日期所在月份的最后一天 //
///////////////////////////////////////////////////////////////////////////
function MonthEnd(Date:TDateTime):TDateTime;
var
Year, Month, Day{, Hour, Min, Sec, MSec}: Word;
T:String;
begin
Result:=0;
DecodeDate(Date, Year, Month, Day);
T:=IntToStr(Year)+'-'+IntToStr(Month)+'-';
case Month of
1,3,5,7,8,10,12:Result:=StrToDate(T+'31');
4,6,9,11 :Result:=StrToDate(T+'30');
2 :if (Year mod 4 =0) and ( Year mod 100 <> 0 )
or (Year mod 400 =0 )
then
Result:=StrToDate(T+'29')
else
Result:=StrToDate(T+'28');
end;
end;

///////////////////////////////////////////////////////////////////////////
// 功能:获得开始日期到结束日期的天数 //
// 入口参数:Date1,Date2 开始日期结束日期 //
// 返回值:天数 //
///////////////////////////////////////////////////////////////////////////

function DateToDateOfDayS(Date1,Date2:TDateTime):Integer;
begin
Result:= (DateTimeToTimeStamp(Date1).Date-DateTimeToTimeStamp(Date2).Date);
if Result<0 then Result:=-Result;
Result:=Result+1;
end;
zhoutian618 2003-10-09
  • 打赏
  • 举报
回复
好。
支持。
wangguan007 2003-10-09
  • 打赏
  • 举报
回复
好!

828

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧