Topic: Help: Get the Current month as Digit MM for TextBox ?

Hello MVDs

I need load my TextBox with the Current month with only 2 Digit like (01) for January, Please help

I try using the Date Time Picker by this code and then copy it to the TextBox but the TextBox show to the full date and I need only the month

procedure Form1_OnShow (Sender: TObject; Action: string);
begin
Form1.DateTimePicker1.Format := 'MM';
Form1.Edit1.Text := DateToStr (Form1.DateTimePicker1.Date); 
end;
Life is like a school;
One can learn and graduate or stay behind.

Re: Help: Get the Current month as Digit MM for TextBox ?

Hi FMR,
You can't just do it as you have tried (Form1.DateTimePicker1.Format := 'MM')
This is just an edit mask that is placed on top of the datetimepicker field and doesn't change the underlying data.
So you need to convert your datetimepicker field to a string and then substring the part that you want (month).
Be aware that the attached example works for a dd/mm/yyyy date format so you might need to amend the code if the date is formatted differently in your country.
Regards,
Derek.

Post's attachments

Attachment icon month only.zip 442.78 kb, 49 downloads since 2024-01-14 

3 (edited by FMR 2024-01-14 21:23:13)

Re: Help: Get the Current month as Digit MM for TextBox ?

Thanks derek

This is a 1st time your solution not fix my problem but you give me a good idea, please check the attached project and let me know if their is any suggestion, just I work for make the month 2 digit on single number like 1 to 01

procedure Form1_OnShow (Sender: TObject; Action: string);
var
Day, Year, Month: Word;
begin

DecodeDate(Form1.DateTimePicker1.DateTime, Year, Month, Day);
Form1.Edit1.Text := (IntToStr(Month));

end;

begin
end.   
Post's attachments

Attachment icon DigitMonth.zip 3.66 kb, 42 downloads since 2024-01-14 

Life is like a school;
One can learn and graduate or stay behind.

Re: Help: Get the Current month as Digit MM for TextBox ?

 if length(month) = 2 then Form1.Edit1.Text  := (IntToStr(Month)) else form1.edit1.text := '0' + (inttostr(month));