00 Votes

Delphi/Lazarus: Date of yesterday

Question by Compi | Last update on 2021-04-02 | Created on 2017-02-25

Is there any way available for getting the date of yesterday using Delphi or Lazarus?

I know that I can get the current date using the function "now" respectively using FormatDateTime('yyyy-mm-dd', now). But what can I do to get yesterday's date?

ReplyPositiveNegative
0Best Answer0 Votes

In addition to the function "now", there is also a function called "yesterday" available which can be used in the same way.

This function returns the date of yesterday as DateTime in which the time is set to 0:00h. Here is a small example:

var
  s: string;
  d: TDateTime;
begin
  d := Yesterday;
  s := FormatDateTime('yyyy-mm-dd', d);
  ShowMessage(s);
end; 

In order to use this function, you will have to add the unit DateUtils to your uses section.

FormatDateTime('yyyy-mm-dd', now - 1);

If you want to calculate the exact time exactly 24 hours ago or if you want to go without the Yesterday-function, you can also just substract 1 from Now. Because TDateTime is a decimal number with the days encoded before the decimal separator (1 corresponds to one day) and the time behind, this can be done without any problems.
Last update on 2021-04-02 | Created on 2017-02-26

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Determine Week Number

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.