方法一:
procedure TForm1.FormCreate(Sender: TObject);
var
firstDay,lastDay:TDateTime;
begin
firstDay := StrToDate(FormatDateTime('yyyy-mm-1',Now)); (* 本月第一天 *)
lastDay := IncMonth(firstDay)-1; (* 本月最后一天 *)
mmo1.Lines.Add(DateTimeToStr(firstDay));
mmo1.Lines.Add(DateTimeToStr(lastDay));
end;
end.
方法二:
USER :DateUtils
使用 StartOfTheMonth 和 EndOfTheMonth 函数获取即可;
procedure TForm1.btn1Click(Sender: TObject);
begin
mmo1.Lines.Add(FormatDateTime('yyyy-mm-dd',StartOfTheMonth(Now)));
mmo1.Lines.Add(FormatDateTime('yyyy-mm-dd',EndOfTheMonth(Now)));
//mmo1.Lines.Add(DateTimeToStr(StartOfTheMonth(Now)));
//mmo1.Lines.Add(DateTimeToStr(EndOfTheMonth(Now)));
end;