22 Votes

Delphi/Lazarus: Extract Day, Month and Year from DateTime-Format

Question by Guest | Last update on 2023-11-24 | Created on 2015-09-21

I have a date in the format TDateTime, but I need the single segments as integer variables. So, I would like to extract day, month and year from the datetime variable in order to store the values in individual integer variables.

Is there any function available for that or can someone help me how to do that?

ReplyPositiveNegativeDateVotes
2Best Answer2 Votes

Delphi as well as Lazarus are coming with a function to do that. The function is called DecodeDate and it can be used in the following way:

var
  ADate: TDateTime;
  AYear, AMonth, ADay: word;
begin
  ADate  := now;	
 
  DecodeDate(ADate, AYear, AMonth, ADay);

  ShowMessage(IntToStr(AYear));
  ShowMessage(IntToStr(AMonth));
  ShowMessage(IntToStr(ADay));
end;

First of all, here, we are saving the current date in the variable ADate. However, at the same time, we have declared the variables AYear, AMonth and ADay in which our year, month and day should be written later. For that, we are calling DecodeDate and we are passing all variables to this function. Finally, we are showing the results with ShowMessage().
Last update on 2023-11-24 | Created on 2015-09-23

ReplyPositive Negative
00 Votes

AYear, AMonth, ADay: word;

(DecodeDate takes word parameters)
2022-10-14 at 02:54

Positive Negative
00 Votes

I have corrected it.

Thank you very much.
2022-10-14 at 02:58

Positive Negative
Reply
Reply

Related Topics

PHP: Current Date and Time

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.