狼行神码

导航

日期时间函数

对日期的处理,一般在有日期限制的共享、商业软件中经常使用到。如果你打算编写一款有日期限制的软件,熟悉使用下面的函数即可以实现。

  1.Date功能说明:返回当前的日期。

  参考实例:

  procedure TForm1.Button1Click(Sender: TObject);

  begin

  Label1.Caption := ''今天是:'' + DateToStr(Date);

  end;

  Label显示为:今天是2005年1月1日。

  2.DateToStr功能说明:将日期型转换为字符型。

  参考实例:

  var

  S: String;

  begin

  S := DateToStr(Date);

  end;

  3.DateTimeToStr功能说明:将DateTime型转换为字符型。

  参考实例:

  var

  S: String;

  begin

  S := DateTimeToStr(Now);

  end;

  4.DayOfTheMonth(所在单元:DateUtils)

  功能说明:获取指定日期的日。

  参考实例:

  Label1.Caption := IntToStr(DayOfTheMonth(Now));

  假设当前日期为2005年1月2日,那么Label将显示为2.

  5.DayOfTheWeek(所在单元:DateUtils)

  功能说明:根据指定日期,获取星期几。

  参考实例:

  Label1.Caption := IntToStr(DayOfTheMonth(Now));

  假设当前日期为2005年1月2日,那么Label将显示为7.根据返回的值来判断是周几。7表示星期天,1为星期一,依类类推。

  6.DayOfTheYear(所在单元:DateUtils)

  功能说明:根据指定日期,获取天数。

  参考实例:

  Label1.Caption := IntToStr(DayOfTheYear(Now));

  假设当前日期为2005年1月2日,那么Label将显示为2.表示是2005年的第2天。

  7.DayOf(所在单元:DateUtils)

  功能说明:根据指定的日期,返回日。

  参考实例:

  Label1.Caption := IntToStr(DayOf(Date));

  假设当前日期为2005年1月2日,那么Label将显示为2.

  8.IsLeapYear功能说明:根据指定的年,判断是否为闰年。可使用YearOf函数获取年。

  参考实例:

  procedure TForm1.Button1Click(Sender: TObject);

  begin

  if IsLeapYear(YearOf(Date)) then ShowMessage(''是闰年'')

  else ShowMessage(''不是闰年'');

  end;

  9.MonthOf(所在单元:DateUtils)

  功能说明:根据指定的日期,返回月份。

  参考实例:

  Label1.Caption := IntToStr(MonthOf(Date));

  假设当前日期为2005年1月2日,那么Label将显示为1.

  10.Now功能说明:返回当前日期及时间。

  参考实例:

  procedure TForm1.Button1Click(Sender: TObject);

  begin

  Label1.Caption := ''现在是:'' + DateTimeToStr(Now);

  end;

  11.YearOf(所在单元:DateUtils)

  功能说明:根据指定的日期,返回年。

  参考实例:

  Label1.Caption := IntToStr(YearOf(Date));

  假设当前日期为2005年1月2日,那么Label将显示为2005.在keypress事件里面,Showmessage(IntToStr(Ord(Key)));这样获得值在刚才的程序中就可以用了!

posted on 2016-10-04 15:01  狼行神码  阅读(201)  评论(0编辑  收藏  举报