delphi中关于时间差的实例


本文整理自网络,侵删。

 很多时候要用到相差多少天,多少周,多少秒,查了一下资料,整理如下:

首先 uses dateutils;

先自己做了个实例,相关代码如下:

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
sd,nd:Tdatetime;
randid:string;
begin
sd := StrtoDatetime('1981-08-08 00:00:00');
nd := now;
memo1.Clear;
memo1.Lines.Add('开始测试时间差:');
memo1.Lines.Add('起始时间:1981-08-08 00:00:00');
memo1.Lines.Add('终止时间:'+datetimetostr(nd));
memo1.Lines.Add('年:'+inttostr(YearsBetween(sd,nd)));
memo1.Lines.Add('月:'+inttostr(MonthsBetween(sd,nd)));
memo1.Lines.Add('周:'+inttostr(WeeksBetween(sd,nd)));
memo1.Lines.Add('日:'+inttostr(DaysBetween(sd,nd)));
memo1.Lines.Add('时:'+inttostr(HoursBetween(sd,nd)));
memo1.Lines.Add('分:'+inttostr(MinutesBetween(sd,nd)));
memo1.Lines.Add('秒:'+inttostr(SecondsBetween(sd,nd)));
memo1.Lines.Add(#13);
memo1.Lines.Add('以秒差为例生成随机数:');
randid := inttostr(SecondsBetween(sd,nd))+inttostr(random(9))+inttostr(random(9))+inttostr(random(9))+inttostr(random(9));//呵呵,很笨的方法。
memo1.Lines.Add(randid);
end;

end.


==================

object Form1: TForm1
Left = 571
Top = 224
BorderIcons = [biSystemMenu, biMinimize]
BorderStyle = bsSingle
Caption = #26102#38388#24046#20989#25968
ClientHeight = 249
ClientWidth = 201
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 8
Top = 8
Width = 185
Height = 25
Caption = #28436#31034#26102#38388#24046#20989#25968
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 8
Top = 40
Width = 185
Height = 201
TabOrder = 1
end
end


相关函数如下:

{ Range query functions }

function YearsBetween(const ANow, AThen: TDateTime): Integer;
function MonthsBetween(const ANow, AThen: TDateTime): Integer;
function WeeksBetween(const ANow, AThen: TDateTime): Integer;
function DaysBetween(const ANow, AThen: TDateTime): Integer;
function HoursBetween(const ANow, AThen: TDateTime): Int64;
function MinutesBetween(const ANow, AThen: TDateTime): Int64;
function SecondsBetween(const ANow, AThen: TDateTime): Int64;
function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;

{ Range spanning functions }
{ YearSpan and MonthSpan are approximates, not exact but pretty darn close }
function YearSpan(const ANow, AThen: TDateTime): Double;
function MonthSpan(const ANow, AThen: TDateTime): Double;
function WeekSpan(const ANow, AThen: TDateTime): Double;
function DaySpan(const ANow, AThen: TDateTime): Double;
function HourSpan(const ANow, AThen: TDateTime): Double;
function MinuteSpan(const ANow, AThen: TDateTime): Double;
function SecondSpan(const ANow, AThen: TDateTime): Double;
function MilliSecondSpan(const ANow, AThen: TDateTime): Double;

找到一篇关于时间函数应用的学习笔记,备份一下。

TDateTime是一个比较常用的类型,用于表达日期时间类型的数据。但是,刚刚接触delphi的新手在使用这个类型的时候往往会不知所措,不知道该怎样使用才能得到自己想要的结果。这里说说我在使用过程中的一点心得,其中大部分的内容是来自于delphi帮助,所以如果看了这篇文章对TDateTime还有什么不清楚的可以去看看Delphi的帮助。另外在这篇文章里我也会告诉大家我使用帮助的心得,这对老手或许是班门弄斧但是对于新手我自认为还是很有帮助的。
一、关于TDateTime
1、TDateTime的基本概念:
根据Delphi的帮助里所说的,TDateTime是date和time例程用来存放date和time变量的,在delphi里TDateTime类型本质上是Double类型的,其中整数位用于表达从1899年12月30日到现在所已经过去的天数,小数部分用于表示当天已经流逝的分数值――有些不明白吧,举个例子:上午6点小数部分就是0.25、中午12点小数部分就是0.5依此类推。
2、常用函数和过程
2.1 Date函数
function Date: TDateTime;
返回当前的日期,实际类型是TDateTime,也就是小数部分为0的DateTime值,小数部分为0代表什么意思呢?根据TDateTime的概念我们知道这代表午夜也就是0点。
2.2 DateToStr
函数的形式:function DateToStr(Date: TDateTime): string;
返回值是个字符串,很有用吧。不过,别高兴的太早,如果你用DateToStr(DateTimePicker.Date),你会发现返回的字符串可能是“04-22-03”,这是为什么呢?这是因为delphi内部有一些系统变量用来定义数字、货币、日期的格式,这些变量delphi称为Currency and date/time formatting variables,有23个。我也没有完全看懂,这里讲几个我搞明白的说说。
var ThousandSeparator: Char;
千分位的符号,一般都是西文的逗号“,”,默认值存放在LOCALE_STHOUSAND变量中(位于windows单元)
var DecimalSeparator: Char;
小数点的符号,一般都是西文的句号“.”,默认值存放在变量LOCALE_SDECIMAL中
var CurrencyDecimals: Byte;
小数点后面保留的位数,一般是2位,默认值存放在变量LOCALE_ICURRDIGITS中
var DateSeparator: Char;
日期的分隔符号,一般使用“-”或“.”,默认值为“-”,默认值存放在变量LOCATE_SDATE中
var ShortDateFormat: string;
短日期格式,一般是“yyyy.mm.dd”,默认值存放在变量LOCALE_SSHORTDATE中
var LongDateFormat: string;
长日期格式,一般是“yyyy.mm.dd”,默认值存放在变量LOCALE_SLONGDATE中
var TimeSeparator: Char;
时间的分隔符,一般是西文的分号“:”,默认值就是这个,默认值存放在变量 LOCALE_STIME中
var TimeAMString: string;
表示上午的字符串,默认为“AM”,默认值存放在变量LOCALE_S1159中
var TimePMString: string;
表示下午的字符串,默认为“PM”,默认值存放在变量LOCALE_S2359中
var ShortTimeFormat: string;
短时间格式,默认值存放在变量LOCALE_ITIME LOCALE_ITLZERO中
var LongTimeFormat: string;
长时间格式,一般为“hh:mm:ss”,默认值存放在变量LOCALE_ITIME and LOCALE_ITLZERO中
所以,如果想开发一个稳健的系统,那么这些系统变量必须要在进入系统的时候进行初始化,下面是我得代码:
SysLocale.PriLangID:=LANG_CHINESE;
DateSeparator:='.';
LongDateFormat:='yyyy.mm.dd';
ShortDateFormat:='yyyy.mm.dd';
TimeSeparator:=':';
LongTimeFormat:='hh:nn:ss';
TimeAMString:='上午';
TimePMString:='下午';
大家一定奇怪,为什么我的LongDateFormat和ShortDateFormat是一样的,老实讲,我也希望能够区分长短日期的格式,但是由于有些关于TDateTime的例程在类型转换的时候要参考ShortDateFormat,所以只能设置成一样的了。
还是继续说DateToStr函数。其他没什么特别的,需要注意的是此函数是根据ShortDateFormat来进行转换的,所以ShortDateFormat我只能设为“yyyy.mm.dd”
说道了DateTimePicker,注意在它的format属性中设置显示格式的时候月份要用“MM”而不是我们通常认为的“mm”。
2.3 StrToDate函数
function StrToDate(const S: string): TDateTime;
这个函数可以看成是DateToStr的逆函数,作用就是把一个字符串转换为TDateTime,参数S必须包括2个或3个数字,用DateSeparator所定义的分隔符分隔,年月日的顺序和格式遵从ShortDateFormat。如果输入的参数不符合日期类型的规定系统会触发EConvertError例外。
2.4 DateTimeToStr函数
function DateTimeToStr(DateTime: TDateTime): string;
把TDateTime转换为字符串,日期格式遵从ShortDateFormat,时间格式遵从LongTimeFormat,如果TDateTime的小数部分是0的话,那么返回的字符串中将没有时间部分。
2.5 StrToDateTime函数
function StrToDateTime(const S: string): TDateTime;
这个函数可以看作是DateTimeToStr函数的逆函数,和StrToDate差不多。
2.6 DateTimeToString过程
procedure DateTimeToString(var Result: string; const Format: string; DateTime: TDateTime);
这个过程的作用和DateTimeToStr和DateToStr一样,特别的地方是可以指定TDateTime的格式,这样就可以得到我们指定格式的日期型字符串了。
2.7 Now,Time,StrToTime等等就不详细介绍了,都可以在帮助里得到相关信息,如果有不明白的地方就写代码测试一下。

二、怎样使用帮助
delphi的帮助不如PowerBuilder的帮助那么详细,并且外面的书也都没有什么很好的详尽讲解
以Delphi6中的DateTimeToStr函数为例子,在代码中我们选中DateTimeToStr,然后按F1就可以得到如下的帮助:【】里是说明
VCL Reference【相关的vcl refrence帮助】
DateTimeToStr function【一般在帮助主题的索引里看到的就是这个】

See also Example【see also链接着相关的内容,Example链接着范例代码】

Converts a TDateTime value to a string.【功能说明,把TDateTime类型转换为String类型】

Unit【所属单元】

SysUtils【说明属于SysUtils】

Category【所属类别】

date/time routines【说明属于date/time例程,并且链接着date/time相关的例程――函数和过程】

function DateTimeToStr(DateTime: TDateTime): string;【函数声名的形式】

Description【描述,详细的说明】

DateTimeToString converts the TDateTime value given by DateTime using the format given by the ShortDateFormat global variable, followed by the time using the format given by the LongTimeFormat global variable. The time is not displayed if the fractional part of the DateTime value is zero. 【DateTimeToStr转换通过DateTime参数传入的TDateTime类型的值为字符串,根据ShortDateFormat全局变量所给定的日期格式,遵循LongTimeFormat所给定的时间格式。如果DateTime的小数部分为零的话返回的字符串中将没有时间部分】

To change how the string is formatted, change ShortDateFormat and LongTimeFormat global date time formatting variables.【要改变字符串的格式,可以通过改变ShortDateFormat和LongTimeFormat全局日期时间格式变量来实现】

三、和数据库中的日期字段
  有人询问怎样在SQL中设定Date/DateTime类型的查询值,这和数据库本身有关。不同的数据库对日期类型的字段一般都有对应的函数和格式,不过应该都支持ANSI标准――谁叫数据库厂商都是美国的。比如,我在Oracle8中要查询日期型字段我一般使用【Select * From LoginTable Where LoginTime >= To_Date('2003.03.01 00:00:00','yyyy.mm.dd hh24:mi:ss')】,MSSQLServer2000中文版的写法是【Select * From LoginTable Where LoginTime >= '2003-03-01 00:00:00'】,SQLServer的联机帮助中讲得比较详细,而且用起来似乎也比较方便,不过Oracle可以自己设定日期格式。
  说句题外话,查询的时候,很多人为了方便往往把日期型的字段用函数转换成字符串,然后和字符串式的日期来进行比较,例如:【Select * From LoginTable Where To_Char(LoginTime,'yyyy.mm.dd hh24:mi:ss') >= '2003.03.01 00:00:00'】,这样的写法语法上没有问题,但是性能上存在很大的隐患。因为,数据库执行这样的查询是要对每一条记录的loginTime字段进行计算然后再比较,如果记录数多,会很慢。

相关阅读 >>

Delphi 打开网页的两种方法

Delphi rest api post sample

Delphi有关asqlite控件支持中文路径的解决方案

Delphi 权限控制(Delphi tactionlist方案)

Delphi 缓冲文件流-tbufferedfilestream tfilestream 性能测试

Delphi中输入法模式imemode属性的解释

Delphi里树形菜单的实现

Delphi的idhttp报508 loop detected错误的原因

Delphi 读文件到十六进制的函数(Delphi 7 下可用)

Delphi xe8 tstylebook的使用

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...