DateTimePicker Date Format Weirdness

  • Thread starter Thread starter Ed Crowley
  • Start date Start date
E

Ed Crowley

I have a DateTimePicker control on my form that is displaying the date
format in UK format (dd/mm/yy).

However, in my code dtpStartDate.Value gives a date in US format, but
dtpStartDate.Value.ToString() gives the date in the required UK format!

Any ideas? All system locale settings are set to British/UK settings.
 
Ed Crowley said:
I have a DateTimePicker control on my form that is displaying the date
format in UK format (dd/mm/yy).

However, in my code dtpStartDate.Value gives a date in US format, but
dtpStartDate.Value.ToString() gives the date in the required UK format!

Any ideas? All system locale settings are set to British/UK settings.

DateTimePicker.Value isn't in *any* particular format - it's just a
DateTime. What do you mean by "gives a date in US format"?
 
Jon Skeet said:
DateTimePicker.Value isn't in *any* particular format - it's just a
DateTime. What do you mean by "gives a date in US format"?

When I assign the value of the DateTimePicker to a DateTime variable, it is
in US format (dd/mm/yy) when I step through the code. When I then try and
pass this date as a parameter to a SQL Server stored procedure it falls over
as the date is invalid (for example today is 11/25/2003 which isn't a valid
UK date, but 11/11/2003 works fine).
 
My guess is that Visual Studio's internal workings are showing through,
and Visual Studio follows US formats, not international or standard ones..
(For instance week enumeration starts with sunday, despite that standards
clearly state monday to be first day of a week)

Are you getting US formats when outputting result somewhere, or only while
debugging?
As mentioned by Jon, Value returns a DateTime object which can be any
format but itself should be local independent.
 
I don't think you can change the way the DateTime object stores it's date
which is as mm/dd/yyyy
I'm not sure how SQL accepts a datetime object, but you may have to change
the sql procedure to perceive that datetime object as mm/dd/yyyy or pass
it something other than a DateTime object. I could be wrong.
 
Ed Crowley said:
When I assign the value of the DateTimePicker to a DateTime variable, it is
in US format (dd/mm/yy) when I step through the code.

That's just how the debugger happens to show it, I suspect. DateTimes
themselves have no intrinsic format.
When I then try and
pass this date as a parameter to a SQL Server stored procedure it falls over
as the date is invalid (for example today is 11/25/2003 which isn't a valid
UK date, but 11/11/2003 works fine).

How are you passing the date? If you specify it as a DateTime rather
than converting it into a string in the first place, it should be fine.
 
Morten Wennevik said:
I don't think you can change the way the DateTime object stores it's date
which is as mm/dd/yyyy

No, DateTime stores it as a long.
 
Jon Skeet said:
That's just how the debugger happens to show it, I suspect. DateTimes
themselves have no intrinsic format.


How are you passing the date? If you specify it as a DateTime rather
than converting it into a string in the first place, it should be fine.

It turned out to be an unrelated problem in the line that calls the stored
procedure. It seems that Visual Studio displays the datetime in US format
while debugging (this threw me off the scent a bit as I assumed this was the
problem), but as you've said the datetime object itself is in no particular
country-specific format.

I wish Microsoft would consider people outside of the USA a little more when
designing their products!

Thanks for your help.
 
My guess is that Visual Studio's internal workings are showing through,
and Visual Studio follows US formats, not international or standard ones.
(For instance week enumeration starts with sunday, despite that standards
clearly state monday to be first day of a week)

Yes. This can be changed by updating the culture.FirstDayOfWeek, but even then,
the built-in DateTimePicker and MonthCalendar do not respect the settings of the
current culture. Go figure.
 
When I assign the value of the DateTimePicker to a DateTime variable, it is
in US format (dd/mm/yy) when I step through the code. When I then try and


U.S. format is mm/dd/yy
 
Back
Top