日期_变换等_一些测试

1、StrToDateTime 的缺陷:

  有的系统 日期格式是这样"2016-08-21",有的又是"2016/08/21",于是 不能使用 StrToDateTime('2016-08-21')这样写死的方式,于是找了一个方式

2、需要 "use DateUtils"

  ZC: 思路:将dt160821清空,就是 1899年12月30日 00时00分00秒000毫秒,然后在这个时间基础上 加上相应的年份/月份/天数,即可得到 目标日期。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DateUtils;

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}

procedure TForm1.Button1Click(Sender: TObject);
var dt160821, dt01 :TDatetime;
begin
  //dt160821 := StrToDate('2016-08-21');
  //dt160821 := now;
  //IncMonth(

  ZeroMemory(@dt160821, sizeof(dt160821));
  Memo1.Lines.Add(datetimetostr(dt160821));  //1899/12/30

  dt01 := IncMonth(dt160821, 1404);
  Memo1.Lines.Add(datetimetostr(dt01));

  // 1899/12/30~2016/12/30 ==> 117年 ==> 1404个月
  // 1899/12/30~2016/07/30 ==> 1499 个月
  dt01 := IncMonth(dt160821, 1399);
  Memo1.Lines.Add(datetimetostr(dt01));

  dt01 := IncDay(dt01, 22); // 2016/07/30 加上 22天 就是 2016/08/21
  Memo1.Lines.Add(datetimetostr(dt01));
end;

procedure TForm1.Button2Click(Sender: TObject);
var dt160821 :TDatetime;
begin
  ZeroMemory(@dt160821, sizeof(dt160821));
  dt160821 := IncMonth(dt160821, 1399);
  dt160821 := IncDay(dt160821, 22);
  Memo1.Lines.Add(datetimetostr(dt160821));
end;

end.

3、

4、

5、

原文地址:https://www.cnblogs.com/CodeSkill/p/5827930.html