-1

I want user to select only dates of the current month I don't know what should put in min and max date property.

Is there any property of something like ' current month '?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

2 Answers2

5

Since both MinDate and MaxDate property takes DateTime, you can set them like;

DateTimePicker1.MinDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

DateTimePicker1.MaxDate = (new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1))
                          .AddMonths(1).AddDays(-1);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • This is Exactly i wanted working fine 1 more question why my all questions are rated in negative like -1 is it really a dumb and silly question i asked ? – Shaikh Noman Nasir May 12 '15 at 06:18
  • @ShaikhNomanNasir: not dumb - you just appeared lazy because you didn't show us what you tried. It looked like you just wanted someone else to do your job for you, for free. – John Saunders May 12 '15 at 06:21
  • oh okay i got it now.. i'll also put what i've been keep trying to do next time thanks – Shaikh Noman Nasir May 12 '15 at 06:24
  • @ShaikhNomanNasir I didn't vote but I think people voted your question because you can _easily_ find your answer in a few minutes with Google. You build your logic already as _I should set `MinDate` property to first day of the current month_ and _I should set `MaxDate` property to last day of the current month_ So, the question turns into; _How can I get the first and last day of the current month?_ In a few second search, you will find http://stackoverflow.com/questions/24245523/getting-the-first-and-day-of-a-month-using-a-given-datetime-instance – Soner Gönül May 12 '15 at 06:24
  • well thats my bad actually i've spent enough time on google but i event got some titles like first day and last day .. I didn't check them because i thought it will user to select only first day and last day not other days .. i'm not doing argument .. i just wanted to be clear so i can avoid my dumb next time :) – Shaikh Noman Nasir May 12 '15 at 06:28
1

Yes, you can use properties MinDate and MaxDate and sets minimum and maximum dates which can be selected in control.

var currentYear = DateTime.Now.Year;
var currentMonth = DateTime.Now.Month;
int firstDayInCurrentMonth = 1;
int lastDayInCurrentMonth = DateTime.DaysInMonth(currentYear, currentMonth);

dateTimePicker.MinDate = new DateTime(currentYear, currentMonth, firstDayInCurrentMonth);
dateTimePicker.MaxDate = new DateTime(currentYear, currentMonth, lastDayInCurrentMonth);
Pawel Maga
  • 5,428
  • 3
  • 38
  • 62
  • This is not how you can restrict a user from selecting dates withing the current month only. – Quality Catalyst May 12 '15 at 06:13
  • what if current date is 12 of the month according to your code user won't be able to select 1 - 11 dates of this month .. – Shaikh Noman Nasir May 12 '15 at 06:14
  • @QualityCatalyst there is no other way to block visible of other dates using standard DateTimePicker control. And show now difference between this answers if you know better. – Pawel Maga May 12 '15 at 06:17