Monday, February 20, 2006

I got a Date with DateTimePicker

Well, Recently i came across one cool problem. Yes! the problem is really cool and what makes me blog here is that there is no simple solution available online, for what i encountered.Thanks for Visual Studio.NET I could rectify the problem myself.

DateTimePicker is a good control that solves most of our calender problems. It could be customized in different ways. Like we can customize the format in which the date appears. Also the look could be changed. Inorder to change the look from ComboBox style to updown-style just change the property "ShowUpDown" to true. It would give you two small updown arrows, that could be used to change the value.

Each of us have our own taste for the way the date should appear, not only taste sometimes our requirements makes us change the display to something else. For example, at times we might need something like :
1/1/2005 10:05:00 AM
and sometimes just :
Jan 1 2005 10:05 AM
What i mean is that there are numerous ways in which DateTimePicker should display the value. So how do you tell the control what format it should display?
The answer is simple, you have the Format property on the DataTimePicker, which can take different values like Long,Short,Time and Custom(Clearly it is an enumeration).Programatically you can change the value as

dtp1.Format=DateTimePickerFormat.Long or
dtp1.Format=DateTimePickerFormat.Short and so on.

The most powerful of these Format values is Custom, which gives us fine grain control about the display. Working with Custom format is complex at first for those who havent worked with it till now. But it is simple then on. The steps are simple.
1. Set the Format property to "Custom"
2. You have another property "CustomFormat" on the DateTimePicker control which gets into action when Custom format is applied.
3. It is a string, so you can set a format as a string here. For example in order to display the date/time as February 21 10:00:00 PM, you can set the custom format as :

dtp1.Format=DateTimePickerFormat.Custom
dtp1.CustomFormat="MMMM dd hh:mm:ss tt"

4. Its over!!! Apply changes, check it out. You see exactly what i showed above.

There are really numerous ways to customize like - you can also display the Day name like Friday, sat or Sun, more and more control is possible. You can even specify 24 hour format, i.e., if its 2 pm it could be shown as 14:00 . How do you do that?
See the following statement :
dtp1.CustomFormat="MMMM dd HH:mm:ss"
The display would be then February 21 14:00:00 ( its in 24 hour format now)

Well i am done with my date, you need further details about the Customformat take a look at this link on MSDN
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatetimepickerclasscustomformattopic.asp

Now let me come to the cool problem i was talking about !!!
Cool Problem - very hot!!
My requirement was that as soon as i load the form which has a DatetimePicker control, the control shoud display the current time ! Well it is rather simple, could be done in two ways.
1. dont set any default value during design time. OR
2. In the form load property, you can work this out as :
a) first set the MaxDate property of dtp to DateTime.Max or anything else you wish. But not that if you set MaxDate as 2004 and you try to set the value greate than the MaxDate, say 2005, you ll encounter an exception. What i do usually is :

dtp1.MaxDate=DateTime.MaxValue.AddDays(-2)

I subtracted -2 because, the DateTimePicker cannot support anything that is after 12/31/9998 12:00:00 AM. if you say

dtp1.MaxDate=DateTime.MaxValue

You encounter this exception:
"DateTimePicker does not support dates after 12/31/9998 12:00:00 AM. Paramter name:value"

And if you know, DateTime.MaxValue is "12/31/9999 11:59:59 PM. That is what DateTimePicker can support is exactly 1 year 1 sec less than the MaxValue. So you can Add -1 year and then add -1 second, but I wanted to be on safer side, so i directly add -2 years(means i remove 2 years from MaxValue which makes it 12/31/9997 11:59:59 PM, which is totally fine.
b) setting the MaxDate is really important because, if you dont do it and then say
dtp1.Value=Date.Now
you shall encounter this exception :
'1/21/2006 1:06:00 PM' is not a valid value for 'Value'. 'Value' should be between MinDate and MaxDate.

Now one can ask if i need to even set MinDate, well it is not actually needed, but what you set at default value is applied as MinDate. So most of the times not needed, incase you set something other than Date.Now to the value of dtp and if you get an exception, you better set even the MinDate property to something else.

All code at single place :
In Form load event

dtp1.MaxDate=DateTime.Now.AddYears(-2)
dtp1.Value=DateTime.Now ' you can set the value.

If you take the exception message 'Value' should be between...... and search in google - all you get is about DataBinding and databases. Well I havent worked on DataBinding much, so no idea about it. But there was no solution for this simple problem, atleast i did not find it.

So I thought it would add my blog little value, if i present some article on how to work with DateTimePicker and a general problem when using it- along with my solution.

Hope you .NET guys find it useful as much as i do. See you soon with another article.

1 comment:

Anonymous said...

Indeed it's still a bit bizar;)