Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time math in Delphi

Tags:

date

time

delphi

I have a pretty unusual problem (for me). I am writing an application that will allow a user to change their system time forward or back either by explicit date (change my date to 6/3/1955) or by increment using buttons (go forward 1 month).

I'm writing this to help some of my users test some software that requires jumps like this in order to simulate real world usage of a billing system.

Changing the time in Delphi is of course very easy:

SetDateTime(2008,05,21,16,07,21,00);

But I'm not sure if Delphi (2006) has any built in helpers for date math, which is one of my least favorite things :)

Any suggestions for the best way to handle this? I'd prefer to stay native as the winapi datetime calls suck.

Thanks!

like image 914
Bruce the Hoon Avatar asked Oct 03 '08 18:10

Bruce the Hoon


2 Answers

As mentioned by gabr and mliesen, have a look at the DateUtils and SysUtils units, useful functions include.

  • IncDay - Add a or subtract a number of days.
  • IncMonth - Add a or subtract a number of months.
  • IncWeek - Add a or subtract a number of weeks.
  • IncYear - Add a or subtract a number of years.
  • EncodeDate - Returns a TDateTime value from the Year, Month, and Day params.
like image 193
stukelly Avatar answered Oct 31 '22 01:10

stukelly


There is plenty of helpers in the DateUtils unit.

like image 25
gabr Avatar answered Oct 31 '22 03:10

gabr