delphi中关于时间差的实例
作者:webfly 日期:2010-02-21
很多时候要用到相差多少天,多少周,多少秒,查了一下资料,整理如下:
首先 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
二、怎样使用帮助
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字段进行计算然后再比较,如果记录数多,会很慢。
上一篇
下一篇

文章来自:
Tags:
相关日志: