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.
Delphi 日期时间函数的更多相关文章
- Delphi日期时间 UNIX
Delphi日期时间,就是常见的 2014-05-02 10:37:35 --------------------------------------------------------------- ...
- mysql与oracle的日期/时间函数小结
前言 本文的日期/时间全部格式化为”2016-01-01 01:01:01“形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时间转字符串函数:to ...
- Oracle日期时间函数大全
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...
- ORACLE 常用函数 日期/时间函数
---------------------------------------------日期/时间函数----------------------------------------------- ...
- MySQL日期时间函数大全(转)
MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1 ...
- Mysql学习笔记(五)数学与日期时间函数
学习内容: 1.数学函数 2.日期时间函数 这些函数都是很常用的函数...在这里进行简单的介绍... 数学函数: mysql); //取绝对值函数 这个函数可安全地使用于 BIGINT 值. mysq ...
- MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数
一.常见字符串函数: 1.CHAR_LENGTH 获取长度(字符为单位) 2.FORMAT 格式化 3.INSERT 替换的方式插入 4.INSTR 获取位置 5.LEFT/RIGHT 取左 ...
- PHP函数之日期时间函数date()使用详解
date()函数是我们在php开发中常碰到并且会使用到的一个日期函数,下面我来给大家介绍date()函数的一些基本扮靓和方法,有需要了解的朋友可进入参考 日期时间函数是PHP 的核心组成部分.无需 ...
- SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学、字符串、日期时间)函数[转]
SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学.字符串.日期时间)函数 --创建表格 create table aa ( UserName varchar(50 ...
随机推荐
- netty : NioEventLoopGroup 源码分析
NioEventLoopGroup 源码分析 1. 在阅读源码时做了一定的注释,并且做了一些测试分析源码内的执行流程,由于博客篇幅有限.为了方便 IDE 查看.跟踪.调试 代码,所以在 github ...
- WPF 自定义TextBox带水印控件,可设置圆角
一.简单设置水印TextBox控件,废话不多说看代码: <TextBox TextWrapping="Wrap" Margin="10" Height=& ...
- js实现图片(高度不确定)懒加载
最近一直在弄广告页,由于广告页几乎都是图片拼凑起来的,为了减少服务器压力和带宽,采用图片懒加载方式,但是我们的图片高度又不确定,所以我在网上下载了echo.js自己改了一下. 大体思路是:让首页先加载 ...
- MSIL实用指南-生成if...else...语句
if...else...语句是非常重要的选择语句,它的生成一般需要ILGenerator的DefineLabel方法和MarkLabel方法,以及Brtrue_S和Br_S指令. 一.DefineLa ...
- C# 文件的一些基本操作
近期程序中经常用到一些文件的操作,现在大致整理一下. 一. 获取文件或文件夹基本信息 1.获取文件信息,组成一个DataTable信息. /// <summary> /// 获取指定目录下 ...
- sql server 常用的查询语句
最近在加强sql 语句的学习,整理一下基本语法,现在记录下 select * from dbo.cangku where city='河南' select distinct(city), cangk ...
- Zookeeper分布式服务协调组件
1.简介 Zookeeper是一个分布式服务协调组件,是Hadoop.Hbase.Kafka的重要组件,它是一个为分布式应用提供一致性服务的组件. Zookeeper的目标就是封装好复杂易出错的服 ...
- mybatis批量插入
<insert id="insertBatch" parameterType="java.util.List" > insert into biz_ ...
- vue中的数据监听以及数据交互
现在我们来看一下vue中的数据监听事件$watch, js代码: new Vue({ el:"#div", data:{ arr:[,,] } }).$watch("ar ...
- java Thread 接口学习
对于程序员来说 Thread应该都不会陌生,具体的接口调用不是本篇的重点.Thread的基本概念及接口的使用:java多线程 下面将更多的从底层实现角度讲一下Thread. Thread的声明如下: ...