1 (edited by v_pozidis 2023-06-26 14:06:44)

Topic: last day of month

how can I set a datetimepicker always the last day of a month?

Re: last day of month

v_pozidis wrote:

how can I set a datetimepicker always the last day of a month?

procedure Form1_OnShow (Sender: TObject; Action: string);
var
Days: integer = DaysInMonth(StrToInt(FormatDateTime('YYYY',NOW)),StrToInt(FormatDateTime('MM',NOW)));
begin
  Form1.MonthCalendar1.Date := StrToDate(IntToStr(Days)+'.'+FormatDateTime('MM',NOW)+'.'+FormatDateTime('YYYY',NOW));
  Form1.DateTimePicker1.DateTime := StrToDate(IntToStr(Days)+'.'+FormatDateTime('MM',NOW)+'.'+FormatDateTime('YYYY',NOW));
end;
Post's attachments

Attachment icon test.rar 2.62 kb, 74 downloads since 2023-06-26 

Re: last day of month

For current date (month)

Form1.DateTimePicker1.Date := incmonth(Date, 1) - strtoint(FormatDateTime('d', incmonth(Date, 1)));

For any selected year month

Form1.DateTimePicker1.Date := incmonth(Form1.DateTimePicker1.Date, 1) - strtoint(FormatDateTime('d', incmonth(Form1.DateTimePicker1.Date, 1)));

and there will be no problems with the separator
Same for the calendar.

Re: last day of month

Thank's sparrow
it works fin.
Can you please explain to me the code. I really like to understand him.



sparrow wrote:

For current date (month)

Form1.DateTimePicker1.Date := incmonth(Date, 1) - strtoint(FormatDateTime('d', incmonth(Date, 1)));

For any selected year month

Form1.DateTimePicker1.Date := incmonth(Form1.DateTimePicker1.Date, 1) - strtoint(FormatDateTime('d', incmonth(Form1.DateTimePicker1.Date, 1)));

and there will be no problems with the separator
Same for the calendar.

5 (edited by sparrow 2023-06-28 07:59:43)

Re: last day of month

increase the month by 1 and subtract from the received value the received value of the days of the increased month.
All of this is calculated in a TDateTime (real) value.


If in understandable language then
1. 2023-06-26 + 1 month = 2023-07-26
2. 2023-07-26 ---> this is the 26th day of the 7th month
3. 2023-07-26 - 26 days = 2023-06-30

By the same principle, dates can be calculated in SQL.

Re: last day of month

Hi Sparrow,
Very nice!  (I tried but it took me 4 steps sad)
Kudos to you.
Derek

Re: last day of month

Hi Derek wink


And here is the SQL

Form1.DateTimePicker1.Date := SQLDateTimeToDateTime(SQLExecute('SELECT date(''now'',''start of month'',''+1 month'',''-1 day'')'));