Delphi/Lazarus: Split DateTime into Day, Month, Year, Hour, Minute and Second
Question by Guest | 2015-10-22 at 08:56
I would like to extract date and time from a TDateTime variable into single integer variables. That is, I would like to have an individual variable for the day, the month, the year, the hours, the minutes and the seconds.
I have tried to write my own function for that, but I have to note success. Does someone may have a ready function for this purpose? Thank you very much.
Related Topics
MySQL: Group Timestamp Column by Month and Year
Tip | 0 Comments
PHP: Current Date and Time
Tutorial | 0 Comments
Delphi/Lazarus: Display current Date and Time
Tip | 0 Comments
JavaScript: Get current Date and Time
Tutorial | 1 Comment
Delphi/Lazarus: 3 Ways to round a Number to X Decimal Places
Tutorial | 7 Comments
Delphi/Lazarus: Command Line Parameter Tutorial Part 1: Sending
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.
It is not necessary to write your own function for this purpose, because Delphi as well as Lazarus do have their own function for that. It is the function DecodeDateTime coming from the unit DateUtils, that of course, have to be added to the the USES-Section to make it work.
Alternatively, we can also use the functions DecodeDate and DecodeTime for the same purpose, here is an example for each function.
uses DateUtils; var ADateTime: TDateTime; AYear, AMonth, ADay: integer; AHour, AMinute, ASecond: integer; begin ADate := now; // version using DecodeDateTime DecodeDateTime(ADate, AYear, AMonth, ADay, AHour, AMinute, ASecond); // or alternative using DecodeDate and DecodeTime DecodeDate(ADate, AYear, AMonth, ADate); DecodeTime(ADate, AHour, AMinute, ASecond); ShowMessage('Year: ' + IntToStr(AYear)); ShowMessage('Month: ' + IntToStr(AMonth)); ShowMessage('Day: ' + IntToStr(ADay)); ShowMessage('Hour: ' + IntToStr(AHour)); ShowMessage('Minute: ' + IntToStr(AMinute)); ShowMessage('Second: ' + IntToStr(ASecond)); end;First of all, we are setting the variable ADate to the current time in order to have a value we can work with. After that, we want to extract the individual parts from this variable (year, month, day, hour, minute and second), for which we have declared some individual integer variables before.
Either, we are using the function DecodeDateTime, to which we can pass all variables at once or we can use the functions DecodeDate and DecodeTime after each other. Both functions are fitting the passed values with the new information.
Subsequently, we have stored the time in the variables AYear, AMonth, ADay, AHour, AMinute and ASecond and we are outputting then after each other with the function ShowMessage() in order to show the result.
2015-10-22 at 14:11