DateTimePicker return wrong format

T

Tony Johansson

Hi!

I have a DateTimePicker that have this format Friday, April 02, 2010 when
the DateTimePicker is displaying the
date which is the correct format according to the specification.

Now to my question is it possible to get the same format which is this
Friday, April 02, 2010 when I
use the Value property on DateTimePicker
string s = dateTimePickerDate.Value.ToString();

The string s in this case get this format 4/2/2010 12:57:16 PM and not the
format that I had expected.

//Tony
 
K

Konrad Neitzel

Hi Tony!

Now to my question is it possible to get the same format which is this
Friday, April 02, 2010 when I
use the Value property on DateTimePicker
string s = dateTimePickerDate.Value.ToString();

I do not understand your problem at the moment. The DateTimePicker.Value is
a DateTime.
And you can decide how to format a DateTime. Just do not use the ToString()
without parameters!

So you can give a CultureInfo as parameter in case you want to have a
special format of a culture and/or you can give a standard or custom date
and time format string.
http://msdn.microsoft.com/en-us/library/97x6twsz.aspx

But I am quite sure, that I understood you wrong, because from what I read
so far you should be an expert in all this culture and format stuff by now
so my information is already known by you :).

With kind regards,

Konrad
 
W

Willem van Rumpt

Tony said:
Hi!

I have a DateTimePicker that have this format Friday, April 02, 2010 when
the DateTimePicker is displaying the
date which is the correct format according to the specification.

Now to my question is it possible to get the same format which is this
Friday, April 02, 2010 when I
use the Value property on DateTimePicker
string s = dateTimePickerDate.Value.ToString();

The string s in this case get this format 4/2/2010 12:57:16 PM and not the
format that I had expected.

//Tony

DateTime.ToString() has several overloads that lets you specify the
exact format. The overload without parameters always formats using the
short date format (of your current culture).
 
T

Tony Johansson

Konrad Neitzel said:
Hi Tony!



I do not understand your problem at the moment. The DateTimePicker.Value
is a DateTime.
And you can decide how to format a DateTime. Just do not use the
ToString() without parameters!

So you can give a CultureInfo as parameter in case you want to have a
special format of a culture and/or you can give a standard or custom date
and time format string.
http://msdn.microsoft.com/en-us/library/97x6twsz.aspx

But I am quite sure, that I understood you wrong, because from what I read
so far you should be an expert in all this culture and format stuff by now
so my information is already known by you :).

With kind regards,

Konrad

The problem is when I get the DateTime value from the DateTimePicker object.
When I get the value by using
DateTime dateTime = dateTimePickerDate.Value;
I don't get the format that I want to.
An example if the date in the DateTimePicker is displaying
Friday, April 02, 2010 I get this date format
4/2/2010 12:57:16 PM
after having used DateTime dateTime = dateTimePickerDate.Value;

So I changed the following two properties in the designer for the
DateTimePicker object
Format = Custom
CustomFormat = dddd,MMMM dd,yyyy

This format means the following
dddd should return The full day-of-week name.
MMMM should return The full month name.
dd should return The two-digit day. Single-digit day values are preceded by
a 0.
yyyy should return The full year (2001 is displayed as "2001").

But I still get the same format which is not the format I want to have
4/2/2010 12:57:16 PM

So I have hoped that when I set a custom format I will get the correct
format when I use
DateTime dateTime = dateTimePickerDate.Value;

But that didn't work.

So what is the best way to do ?
I can use string manipulation on this string 4/2/2010 12:57:16 PM
to get Friday, April 02, 2010 but that is a very bad solution..

//Tony
 
T

Tony Johansson

Willem van Rumpt said:
DateTime.ToString() has several overloads that lets you specify the exact
format. The overload without parameters always formats using the short
date format (of your current culture).

Excellent explained It works now I just did
string s = dateTimePickerDate.Value.ToString("dddd,MMMM dd, yyyy");

//Tony
 
K

Konrad Neitzel

Hi Tony!

The problem is when I get the DateTime value from the DateTimePicker
object.
When I get the value by using
DateTime dateTime = dateTimePickerDate.Value;
I don't get the format that I want to.

A DateTime does not have a Format. It just stores a DateTime. When you
display it, the application that displays it, has to decide how it should be
displayed.

So the DateTimePicker needs your input, on how to display it.
Same with the ToString().

And if you do not give this information, some standards are used. (e.g.
CultureInfo.CurrentCulture)

Some other important point (but please do not be confused by it!):
And to make it worse: It is not just a dumb value without any format - it
also has no idea about the timezone. So when we all agree to meet next
monday at 1 PM somewhere(e.g. a conference call), it would be a desaster
because people have different timezones. DateTimeOffset would be nice or
when you store DateTime, you could simply store everything in UTC.

With kind regards,

Konrad
 
K

Konrad Neitzel

Hi Tony!

Excellent explained It works now I just did
string s = dateTimePickerDate.Value.ToString("dddd,MMMM dd, yyyy");

Maybe you even want to use
string s =
dateTimePickerDate.Value.ToString(dateTimePickerDate.CustomFormat);
instead - in case you always want to have the String in the same Format as
you have it in the DateTimePicker.

And the DateTimePicker also a a Text Property. So if you want to have the
same Format: Just use the Text Property instead?

With kind regards,

Konrad
 
T

Tony Johansson

Konrad Neitzel said:
Hi Tony!



Maybe you even want to use
string s =
dateTimePickerDate.Value.ToString(dateTimePickerDate.CustomFormat);
instead - in case you always want to have the String in the same Format as
you have it in the DateTimePicker.

And the DateTimePicker also a a Text Property. So if you want to have the
same Format: Just use the Text Property instead?

With kind regards,

Konrad
As you said I can use string s =
dateTimePickerDate.Value.ToString(dateTimePickerDate.CustomFormat);
but then I must specify this dddd,MMMM dd, yyyy in the customformat
property.
I prefer to specify the format explicitly in the ToString method instead.

//Tony
 
T

Tony Johansson

Tony Johansson said:
As you said I can use string s =
dateTimePickerDate.Value.ToString(dateTimePickerDate.CustomFormat);
but then I must specify this dddd,MMMM dd, yyyy in the customformat
property.
I prefer to specify the format explicitly in the ToString method instead.

//Tony

I noticed as you mentioned that I can use the Text property which use the
same format as the display format for the DateTimePicker has which is
actually what I want here.

//Tony
 
M

Mihai N.

I prefer to specify the format explicitly in the ToString method instead.

That's bad, because it is not locale sensitive.
If you change the formatting locale on the system, the date picker
will respect that, but your stuff (with hard-coded format) will not.
 
P

Peter Duniho

Mihai said:
I prefer to specify the format explicitly in the ToString method instead.

That's bad, because it is not locale sensitive. [...]

First, not being locale-sensitive is only bad if that's contrary to the
intended design. Being locale-sensitive is not always the desired
implementation.

Second, it's not completely true that a custom format isn't locale
sensitive. In particular, the date and time separators are dependent on
the current culture; only the exact arrangement of the various time
components depends on the custom format (unless, of course, one uses a
quoted string to hard-code a separator), and of course even within a
given culture, there is not generally "one right way" to make that
arrangement.

It is not correct to say in an absolute way (as you've done) that using
a custom format is bad.

Pete
 
M

Mihai N.

First, not being locale-sensitive is only bad if that's contrary to the
intended design. Being locale-sensitive is not always the desired
implementation.

The request was to get the same format as the date picker.
The date picker is locale sensitive.

sensitive. In particular, the date and time separators are dependent on
the current culture; only the exact arrangement of the various time
components depends on the custom format (unless, of course, one uses a
quoted string to hard-code a separator), and of course even within a
given culture, there is not generally "one right way" to make that
arrangement.

Have you read the thread?
He used this:
string s = dateTimePickerDate.Value.ToString("dddd,MMMM dd, yyyy");

So the order of the fields, *and* the separators, are now hard-coded.
And the field order is a locale sensitive thing.
Force it to the US centric Month-day and it is not locale sensitive,
no matter if the month name is localized into French, or German,
or whatever.


It is not correct to say in an absolute way (as you've done) that using
a custom format is bad.

Unless you get that format directly from the user (like you can do in Excel),
it is bad.
 
R

Ravinder Raturi

Thats easy..

string s = Convert.ToString(DateTime.Today.ToString("ddd ,dd MMM yy"));

Thats how it will be done

Harry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top