频道分类

DELPHI如何生成一段时间内的所有日期

作者:admin 来源: 日期:2020/2/14 21:39:49 人气: 标签:

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.Button1Click(Sender: TObject);
var
  dateList:Array of string;
  dateBetween:Integer;
  I:Integer;
  s:string;
begin
  dateBetween:=daysBetween(strtodatetime('2018/01/01'),strtodatetime('2020/02/14'));
  setlength(dateList,dateBetween+1);
  for I:=0 to dateBetween do
  begin
    s:=formatdatetime('yyyy/mm/dd',incday(strtodatetime('2018/01/01'),I));
    Memo1.Lines.Add(s);
  end;

  setlength(dateList,0);
end;


//感谢黑夜杀手提供代码   Delphi/黑夜(281309196)
procedure TForm1.Button2Click(Sender: TObject);
var
d1,d2:TDate;
i,days:integer;
begin
Memo1.Clear;
d1:=EncodeDate(2018,1,1);
d2:=EncodeDate(2020,2,14);
days:=DaysBetween(d1,d2);
for I := 0 to days do
  begin
    Memo1.Lines.Add(FormatDateTime('yyyy-mm-dd',d1));
    d1:=d1+1;
  end;

end;

end.
来源:https://bbs.csdn.net/topics/190068930