I want to disable the past days in date time picker control...they must be appear on the calender but they are in disable state.I am tried...but the all past days are disappeared from the calender.any idea..
My code is:
dateTimePicker1.MinDate = DateTime.Now.Date;
thanks.
Try to Use Following :
dateTimePicker1.MinDate = new DateTime(year, month,day);
dateTimePicker1.MaxDate = new DateTime(year, month,day);
dateTimePicker1.ShowUpDown = true;
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MMMM dd";
To disable Days before the first day of current month:
dateTimePicker1.MinDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
Limit Future Date :
MaxDate is used as follows
dateTimePicker1.MaxDate = DateTime.Now.AddDays(number of days);
Related
I have added the calendar form and it won't let me select 2 dates. It assigns every click to the start date so when I try this it always tells me the difference is 1 and the start date is always changed to whatever my next click is?
Is it possible to have it default to today's date for the start date and then have every other click determine the end date? When I tried to assign today's date within the datechanged event handler it wouldn't let me change the month because it kept focusing on the startdate?
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
numDays = Convert.ToInt32((monthCalendar1.SelectionEnd - monthCalendar1.SelectionStart).TotalDays);
MessageBox.Show("num " + numDays);
}
// Sets the Month Calenders Min & Max to days in current month.
DateTime dt = DateTime.Today;
DateTime firstDay = new DateTime(dt.Year, dt.Month, 1, 0, 0, 0);
DateTime lastDay = new DateTime(dt.Year, dt.Month, DateTime.DaysInMonth(dt.Year, dt.Month));
dateMonthCalender.MinDate = firstDay;
dateMonthCalender.MaxDate = lastDay;
The above will set min and max to days in current month
If you want to set the min to today
dateMonthCalender.MinDate = DateTime.Now;
Hope that helps..
Okay so declare two ints and assign one to selected day and the other to today and create yourself a method
int selectedDay;
int todayValue;
DateTime firstDay = new DateTime(dt.Year, dt.Month, 1, 0, 0, 0);
DateTime today = DateTime.Today;
string todayShort = today.ToShortDateString();
string thisDay = todayShort.Substring(0, 2);
todayValue = Convert.ToInt32(thisDay);
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
// Shorten date format to day and assign it.
string dMC = dateMonthCalender.SelectionRange.Start.ToShortDateString();
string takeDMCDay = dMC.Substring(0, 2);
selectedDay = Convert.ToInt32(takeDay);
}
I used the following code in order to change Persian date to Gregorian date. for example it changes 1393/05/05 to 2014/05/26.
if (MultiView6.ActiveViewIndex == 1)
{
date = Convert.ToDateTime(txtDate.Text);
string change = date.ToString("yyyy/MM/dd");
int day1 = Convert.ToInt32(change.Substring(8, 2));
int mon1 = Convert.ToInt32(change.Substring(5, 2));
int year1 = Convert.ToInt32(change.Substring(0, 4));
PersianCalendar pc = new PersianCalendar();
change = (pc.ToDateTime(year1, mon1, day1, 0, 0, 0, 0).ToString("yyyy/MM/dd").Substring(0, 10));
date = Convert.ToDateTime(change);
}
however for some special dates such as 1393/02/29 the code stops working and I receive the above error from the first line of my code:
String was not recognized as a valid DateTime.
I suppose I would have the same problem if I want to convert 2014/05/19 to Persian date (which is 1393/02/29)....
is there a way to fix this error?
year, month and day are in PersianCalendar and you can't use them in DateTime. if you want to convert from PersianCalendar to DateTime, you have to do this:
DateTime d1=p.ToDateTime(year, month, day, 0, 0, 0, 0);
=====================================================
in your new code you have another mistake. variables day1, mon1 and year1 are in Gregorian calendar and you can't use them in PersianCalendar.ToDateTime().
All you need is to use constructor accepting Calendar - DateTime(int year, int month, int day, Calendar calendar).
string persianDateString = "1393/02/29";
string[] persianDateParts = persianDateString.Split('/');
int persianYear = int.Parse(persianDateParts[0]);
int persianMonth = int.Parse(persianDateParts[1]);
int persianDay = int.Parse(persianDateParts[2]);
PersianCalendar pc = new PersianCalendar();
DateTime date = new DateTime(persianYear, persianMonth, persianDay, pc);
If I have var olddate = DateTime.Parse('05/13/2012');
and I want to get var newdate = (the first of the month after olddate, 06/01/2012 in this case);
What would I code? I tried to set the month+1 but month has no setter.
Try this:
olddate = olddate.AddMonths(1);
DateTime newDate = new DateTime(olddate.Year, olddate.Month, 1,
0, 0, 0, olddate.Kind);
This won't ever cause out-of-range errors, and will preserve the DateTime's Kind.
dt = dt.AddMonths(1.0);
dt = new DateTime(dt.Year, dt.Month, 1, 0, 0, 0, dt.Kind);
You have to define the Month and Year rightly, and after set the 1ยช day. Try this:
// define the right month and year of next month.
var tempDate = oldDate.AddMonths(1);
// define the newDate with the nextmonth and set the day as the first day :)
var newDate = new DateTime(tempDate.Year, tempDate.Month, 1); //create
Try this simple one-liner:
var olddate = DateTime.Parse("05/13/2012");
var newdate = olddate.AddDays(-olddate.Day + 1).AddMonths(1);
// newdate == DateTime.Parse("06/01/2012")
lots of examples...pick your posion ;)
var olddate = DateTime.Parse("05/12/2012");
int currentDay = ((DateTime)olddate).Day;
//can always replace the while loop and just put a 1 for current day
while(currentDay != 1)
currentDay--;
var newdate = (DateTime.Parse(olddate.AddMonths(1).Month.ToString() + "/" + currentDay.ToString() + "/" + olddate.AddMonths(1).Year.ToString()));
How do i set the textbox value to last day of last month(to end of previous month), using today's date.
for example:
if today is 23/03/2012 textbox value should be 29/02/2012
if come next month and date is 12/04/2012 then textbox value should be 31/03/2012 and so on
Thanks
Take the first day of the current month and subtract 1:
DateTime value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
DateTime date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
textBox1.Text = date.ToShortDateString();
Use DateTime.DaysInMonth to accomplish this:
var daysInMonth = DateTime.DaysInMonth(dt.Year, dt.Month - 1);
var lastDayInMonth = new DateTime(dt.Year, dt.Month - 1, daysInMonth);
textBox1.Text = lastDayInMonth.ToString("dd/MM/yyyy");
In C#:
DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddDays(-1);
Then call .ToString() and pass in whatever format you like.
Get the first day of the month and subtract one day.
DateTime lastDayOfThePreviousMonth = dateSelected.AddDays(-dateSelected.Day);
I want to increase time to current time.
For example, I have the time of the problem and the expected time to complete them. How can I add to it?
(DateTime.Now.ToShortDateString() + ...)
You can use other variables:
DateTime otherDate = DateTime.Now.AddMinutes(25);
DateTime tomorrow = DateTime.Now.AddHours(25);
You can use the operators +, -, +=, and -= on a DateTime with a TimeSpan argument.
DateTime myDateTime = DateTime.Parse("24 May 2009 02:19:00");
myDateTime = myDateTime + new TimeSpan(1, 1, 1);
myDateTime = myDateTime - new TimeSpan(1, 1, 1);
myDateTime += new TimeSpan(1, 1, 1);
myDateTime -= new TimeSpan(1, 1, 1);
Furthermore, you can use a set of "Add" methods
myDateTime = myDateTime.AddYears(1);
myDateTime = myDateTime.AddMonths(1);
myDateTime = myDateTime.AddDays(1);
myDateTime = myDateTime.AddHours(1);
myDateTime = myDateTime.AddMinutes(1);
myDateTime = myDateTime.AddSeconds(1);
myDateTime = myDateTime.AddMilliseconds(1);
myDateTime = myDateTime.AddTicks(1);
myDateTime = myDateTime.Add(new TimeSpan(1, 1, 1));
For a nice overview of even more DateTime manipulations see THIS
You can also add a TimeSpan to a DateTime, as in:
date + TimeSpan.FromHours(8);
Please note that you may add - (minus) sign to find minutes backwards
DateTime begin = new DateTime();
begin = DateTime.ParseExact("21:00:00", "H:m:s", null);
if (DateTime.Now < begin.AddMinutes(-15))
{
//if time is before 19:45:00 show message etc...
}
and time forward
DateTime end = new DateTime();
end = DateTime.ParseExact("22:00:00", "H:m:s", null);
if (DateTime.Now > end.AddMinutes(15))
{
//if time is greater than 22:15:00 do whatever you want
}