Delphi 日期时间函数

DateUtils.IncYear();
DateUtils.IncMonth();
DateUtils.IncWeek();
DateUtils.IncDay();
DateUtils.IncHour();
DateUtils.IncMinute();
DateUtils.IncSecond();
DateUtils.IncMilliSecond();

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var
 t1,t2: TDateTime;
 i: Int64;
 d: Double;
begin
 t1 := StrToDateTime('2011-1-1 1:1:1');

 t2 := IncYear(t1);
 ShowMessage(DateTimeToStr(t2)); //2012-1-1 1:01:01

 t2 := IncYear(t1, 3);
 ShowMessage(DateTimeToStr(t2)); //2014-1-1 1:01:01

 t2 := IncMonth(t1);
 ShowMessage(DateTimeToStr(t2)); //2011-2-1 1:01:01

 t2 := IncWeek(t1);
 ShowMessage(DateTimeToStr(t2)); //2011-1-8 1:01:01

 t2 := IncDay(t1);
 ShowMessage(DateTimeToStr(t2)); //2011-1-2 1:01:01

 t2 := IncHour(t1);
 ShowMessage(DateTimeToStr(t2)); //2011-1-1 2:01:01

 t2 := IncMinute(t1);
 ShowMessage(DateTimeToStr(t2)); //2011-1-1 1:02:01

 t2 := IncSecond(t1);
 ShowMessage(DateTimeToStr(t2)); //2011-1-1 1:01:02

 t2 := IncMilliSecond(t1);
 ShowMessage(FormatDateTime('yyyy-m-d h:n:s:zzz', t2)); //2011-1-1 1:01:01:001
end;

end.

StartOfAYear ... StartOfTheYear ... EndOfAYear ... EndOfTheYear ... 每年、月、周、日的开始与结束的时间。

{参数是指定的年、月、周、日}
DateUtils.StartOfAYear 
DateUtils.StartOfAMonth
DateUtils.StartOfAWeek 
DateUtils.StartOfADay

{参数是 TDateTime}
DateUtils.StartOfTheYear 
DateUtils.StartOfTheMonth
DateUtils.StartOfTheWeek 
DateUtils.StartOfTheDay

{参数是指定的年、月、周、日}
DateUtils.EndOfAYear 
DateUtils.EndOfAMonth
DateUtils.EndOfAWeek 
DateUtils.EndOfADay

{参数是 TDateTime}
DateUtils.EndOfTheYear
DateUtils.EndOfTheMonth
DateUtils.EndOfTheWeek
DateUtils.EndOfTheDay

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var
 dt,t1,t2,t3,t4: TDateTime;
 s1,s2,s3,s4: string;
begin
 dt := StrToDateTime('2009-5-20 11:22:33');

 t1 := StartOfAYear(2009);
 t2 := StartOfTheYear(dt);
 t3 := EndOfAYear(2009);
 t4 := EndOfTheYear(dt);

 s1 := FormatDateTime('yyyy-m-d h:n:s:zzz', t1); //2009-1-1 0:0:0:000
 s2 := FormatDateTime('yyyy-m-d h:n:s:zzz', t2); //2009-1-1 0:0:0:000
 s3 := FormatDateTime('yyyy-m-d h:n:s:zzz', t3); //2009-12-31 23:59:59:999
 s4 := FormatDateTime('yyyy-m-d h:n:s:zzz', t4); //2009-12-31 23:59:59:999

 t1 := StartOfAMonth(2009, 5); { 2009 年 5 月 }
 t2 := StartOfTheMonth(dt);
 t3 := EndOfAMonth(2009, 5);
 t4 := EndOfTheMonth(dt);

 s1 := FormatDateTime('yyyy-m-d h:n:s:zzz', t1); //2009-5-1 0:0:0:000
 s2 := FormatDateTime('yyyy-m-d h:n:s:zzz', t2); //2009-5-1 0:0:0:000
 s3 := FormatDateTime('yyyy-m-d h:n:s:zzz', t3); //2009-5-31 23:59:59:999
 s4 := FormatDateTime('yyyy-m-d h:n:s:zzz', t4); //2009-5-31 23:59:59:999

 t1 := StartOfAWeek(2009, 21); { 2009 年第 21 周 }
 t2 := StartOfTheWeek(dt);
 t3 := EndOfAWeek(2009, 21);
 t4 := EndOfTheWeek(dt);

 s1 := FormatDateTime('yyyy-m-d h:n:s:zzz', t1); //2009-5-18 0:0:0:000
 s2 := FormatDateTime('yyyy-m-d h:n:s:zzz', t2); //2009-5-18 0:0:0:000
 s3 := FormatDateTime('yyyy-m-d h:n:s:zzz', t3); //2009-5-24 23:59:59:999
 s4 := FormatDateTime('yyyy-m-d h:n:s:zzz', t4); //2009-5-24 23:59:59:999

 t1 := StartOfADay(2009, 140); { 2009 年第 140 天 }
 t2 := StartOfTheDay(dt);
 t3 := EndOfADay(2009, 140);
 t4 := EndOfTheDay(dt);

 s1 := FormatDateTime('yyyy-m-d h:n:s:zzz', t1); //2009-5-20 0:0:0:000
 s2 := FormatDateTime('yyyy-m-d h:n:s:zzz', t2); //2009-5-20 0:0:0:000
 s3 := FormatDateTime('yyyy-m-d h:n:s:zzz', t3); //2009-5-20 23:59:59:999
 s4 := FormatDateTime('yyyy-m-d h:n:s:zzz', t4); //2009-5-20 23:59:59:999
end;

end.

WeeksInAYear、WeeksInYear、DaysInAYear、DaysInAMonth、DaysInYear、DaysInMonth - 获取指定年月的周、日数。

DateUtils.DaysInYear();
DateUtils.DaysInMonth();
DateUtils.DaysInAYear();
DateUtils.DaysInAMonth();
DateUtils.WeeksInYear();
DateUtils.WeeksInAYear();

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var
 dt: TDateTime;
 w: Word;
begin
 dt := StrToDateTime('2009-5-20 11:22:33');

 {指定日期所在年的总天数}
 w := DaysInYear(dt);    //365

 {指定日期所在月的总天数}
 w := DaysInMonth(dt);    //31

 {指定年的总天数}
 w := DaysInAYear(2009);   //365

 {指定年、指定月的总天数}
 w := DaysInAMonth(2009, 5); //31

 {指定日期所在年的总周数}
 w := WeeksInYear(dt);    //53

 {指定年的总周数}
 w := WeeksInAYear(2009);  //53
end;

end.

WithinPastYears、WithinPastMonths、WithinPastWeeks、WithinPastDays ... 判断两个时间差是否在一个指定范围内。

DateUtils.WithinPastYears();
DateUtils.WithinPastMonths();
DateUtils.WithinPastWeeks();
DateUtils.WithinPastDays();
DateUtils.WithinPastHours();
DateUtils.WithinPastMinutes();
DateUtils.WithinPastSeconds();
DateUtils.WithinPastMilliSeconds(); 

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var
 t1,t2: TDateTime;
 b: Boolean;
begin
 t1 := StrToDateTime('2009-5-20 11:22:33');
 t2 := StrToDateTime('2009-5-21 11:22:33');

 {两个时间差是否在 24 年内}
 b := WithinPastYears(t1, t2, 24); //True

 {两个时间差是否在 24 个月内}
 b := WithinPastMonths(t1, t2, 24); //True

 {两个时间差是否在 24 个周内}
 b := WithinPastWeeks(t1, t2, 24); //True

 {两个时间差是否在 24 天内}
 b := WithinPastDays(t1, t2, 24);  //True

 {两个时间差是否在 24 个小时内}
 b := WithinPastHours(t1, t2, 24); //True

 {两个时间差是否在 24 分钟内}
 b := WithinPastMinutes(t1, t2, 24);   //False

 {两个时间差是否在 24 秒内}
 b := WithinPastSeconds(t1, t2, 24);   //False

 {两个时间差是否在 24 毫秒内}
 b := WithinPastMilliSeconds(t1, t2, 24); //False

// ShowMessage(BoolToStr(b, True));
end;

end.

RecodeDateTime、RecodeDate、RecodeTime、RecodeYear ... 修改时间。

DateUtils.RecodeDateTime();
DateUtils.RecodeDate();
DateUtils.RecodeTime();
DateUtils.RecodeYear();
DateUtils.RecodeMonth();
DateUtils.RecodeDay();
DateUtils.RecodeHour();
DateUtils.RecodeMinute();
DateUtils.RecodeSecond();
DateUtils.RecodeMilliSecond();
DateUtils.TryRecodeDateTime();

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
const
 st = 'yyyy-m-d h:n:s:z';
var
 t1, t2: TDateTime;
begin
 t1 := EncodeDateTime(2001, 1, 1, 1, 1, 1, 1);

 t2 := RecodeDateTime(t1, 2009, 5, 21, 11, 22, 33, 999);
 ShowMessage(FormatDateTime(st, t2));  //2009-5-21 11:22:33:999

 t2 := RecodeDate(t1, 2009, 5, 21);
 ShowMessage(FormatDateTime(st, t2));  //2009-5-21 1:1:1:1

 t2 := RecodeTime(t1, 11, 22, 33, 999);
 ShowMessage(FormatDateTime(st, t2));  //2009-1-1 11:22:33:999

 t2 := RecodeYear(t1, 2009);
 ShowMessage(FormatDateTime(st, t2));  //2009-1-1 1:1:1:1

 t2 := RecodeMonth(t1, 5);
 ShowMessage(FormatDateTime(st, t2));  //2001-5-1 1:1:1:1

 t2 := RecodeDay(t1, 21);
 ShowMessage(FormatDateTime(st, t2));  //2001-1-21 1:1:1:1

 t2 := RecodeHour(t1, 11);
 ShowMessage(FormatDateTime(st, t2));  //2001-1-1 11:1:1:1

 t2 := RecodeMinute(t1, 22);
 ShowMessage(FormatDateTime(st, t2));  //2001-1-1 1:22:1:1

 t2 := RecodeSecond(t1, 33);
 ShowMessage(FormatDateTime(st, t2));  //2001-1-1 1:1:33:1

 t2 := RecodeMilliSecond(t1, 999);
 ShowMessage(FormatDateTime(st, t2));  //2001-1-1 1:1:1:999

 if TryRecodeDateTime(t1, 2009, 5, 21, 11, 22, 33, 999, t2) then
  ShowMessage(FormatDateTime(st, t2));  //2009-5-21 11:22:33:999
end;

end.

IsValidDateTime、IsValidDate、IsValidTime、IsValidDateDay ... 判断时间是否合法。

DateUtils.IsValidDateTime
DateUtils.IsValidDate
DateUtils.IsValidTime
DateUtils.IsValidDateDay
DateUtils.IsValidDateWeek
DateUtils.IsValidDateMonthWeek

//可用下面几个过程抛出异常:
DateUtils.InvalidDateDayError
DateUtils.InvalidDateMonthWeekError
DateUtils.InvalidDateTimeError
DateUtils.InvalidDateWeekError
DateUtils.InvalidDayOfWeekInMonthError

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var
 b: Boolean;
begin
 b := IsValidDateTime(2009, 5, 21, 11, 22, 33, 999); //True
 b := IsValidDateTime(2009, 5, 21, 11, 22, 33, 9999); //False

 b := IsValidDate(2009, 5, 21); //True
 b := IsValidDate(2009, 5, 32); //False

 b := IsValidTime(11, 22, 33, 0); //True
 b := IsValidTime(11, 22, 61, 0); //False

 b := IsValidDateDay(2009, 141); //True
 b := IsValidDateDay(2009, 366); //False

 b := IsValidDateWeek(2009, 21, 7); //True
 b := IsValidDateWeek(2009, 21, 8); //False

 b := IsValidDateMonthWeek(2009, 5, 3, 7); //True
 b := IsValidDateMonthWeek(2009, 5, 3, 8); //False

// ShowMessage(BoolToStr(b, True));
end;

end.

IsSameDay、IsToday - 判断是不是同一天、判断是不是今天。

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var
 t1,t2,t3,t4: TDateTime;
 b: Boolean;
begin
 t1 := StrToDateTime('2009-1-21');
 t2 := StrToDateTime('2009-1-21');
 t3 := StrToDateTime('2009-5-21');
 t4 := Now;

 {判断是不是同一天}
 b := IsSameDay(t1, t2); //True
 b := IsSameDay(t1, t3); //False

 {判断给定时间和当前时间是不是同一天}
 b := IsToday(t1); //False
 b := IsToDay(t4); //True

// ShowMessage(BoolToStr(b, True));
end;

end.

Yesterday、Today、Tomorrow - 昨天、今天、明天。

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var
  t1,t2,t3: TDateTime;
begin
  t1 := Yesterday;
  t2 := Today;     //同 SysUtils.Date;
  t3 := Tomorrow;

  ShowMessageFmt('%s, %s, %s', [DateToStr(t1), DateToStr(t2), DateToStr(t3)]);
  {2009-5-20, 2009-5-21, 2009-5-22}
end;

end.

上一篇:IT关键词,发现与更新,点成线,线成面,面成体


下一篇:springboot整合多数据源及事物