1

Using the TCalendar, it has a option on top of calendar for previous month , next month buttons. Then it has a dropdown for month and a drop down for year. I cant seem to find out how to get this data when it changes.

Month := Calendar1.???
Year := calendar1.???

Also how to know when the previous, next, month dropdown and year dropdown is selected? On change?

To be clear, I can get month and year from selected date. The question is if they change month and year but don't select day. Then date has not changed so I can't use date to get the current month and year that is in view on the calendar

2
  • either one procedure TForm1.Calendar1Change(Sender: TObject); begin Label1.Caption:=DateToStr( Calendar1.Day); end; procedure TForm1.Calendar1Click(Sender: TObject); begin // Label1.Caption:=DateToStr( Calendar1.Day); end; Sep 23 at 15:53
  • @shlomoAbuisak this is for date. If they change month or year but don't select a day. They this would not work.
    – user41758
    Sep 23 at 17:04

1 Answer 1

1

For example, to output the new date to a memo:

Memo1.Lines.Add(DateToStr(Calendar1.Date));

To output to the memo day, month and/or year, add System.DateUtils to uses and write code like:

Memo1.Lines.Add(IntToStr(DayOf( Calendar1.Date)));
Memo1.Lines.Add(IntToStr(MonthOf( Calendar1.Date)));
Memo1.Lines.Add(IntToStr(YearOf( Calendar1.Date)));

The OnChange event is the one that gets triggered whenever the date of the calendar is changed.

8
  • This is assuming they change the date or click on a new day. If you just change month. Or year with out selecting a day. The date has not changed.
    – user41758
    Sep 23 at 17:01
  • @user41758 I'm not shure I get your point. Whatever you change, day, month, year, alone or in combination, the calendar triggers the OnChange event. Or can you give me an example of when it doesn't? Sep 23 at 17:12
  • When you change month or year, the calendar1.date does not change till you select a day. So if they change date to 2024, the calendar view will show that year. But calendar1.date has not changed. I want to know year in the current view. not based on calendar1.date because it's not updated
    – user41758
    Sep 23 at 17:18
  • I don't know where you have got the idea that a the Calendar1.Date would not be affected by a change of the year. A TDate specifically defines a date consisting of a year, a month and a day, because it is a calculation of days since 30.12.1899. Even if you change only the year, say one year, the value will change with 365 (366 on leap years), for one month it will change by 28, 29, 30 or 31 depending on the month (and possibly leap year), and a change of one day will change the value with 1. Sep 23 at 18:12
  • I showed you with some example code how to extract the various parts of a date value using functions in the System.DateUtils unit. Why dont you try my code and see for your self? Sep 23 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.