0

I have a DateTimePicker in WPF. The dateTimePicker give me an European Format

In my DateTimePicker, i select "01/02/2014" ( in european format )

I would like to read the value, and convert it in a shortDateString with the US culture.

I have done this :

CultureInfo m_UsCulture = new CultureInfo("en-us");
string str = m_dDate.Date.ToShortDateString().ToString(m_UsCulture);

The str variable is : "01/02/2014" instead of "2/1/2014"

The ShortDateString appear to be OK, but not the "ToString(m_UsCulture);".

I would like to do this in a one line please. Have you an idea for this error ?

Thanks a lot :)

Walter Fabio Simoni
  • 5,671
  • 15
  • 55
  • 80
  • 1
    What's the value in it being in a single line? – Oded Mar 15 '13 at 15:16
  • What will you do with the string once you have it? – Chris Dunaway Mar 15 '13 at 15:16
  • 2
    You're converting a string to a string - `ToShortDateString().ToString(m_UsCulture)` so how does it apply culture to the date? – Lloyd Mar 15 '13 at 15:17
  • What @Lloyd said, the `ToString(m_UsCulture)` is applying the culture to a `String`, not `DateTime`, it's the same as: `"01/02/2014".ToString(m_UsCulture)` – Peter Ritchie Mar 15 '13 at 15:19
  • 2
    OT: There is no such thing as European Format. We inhabitants of the roughly 50 countries which make up Europe have many different date formatting rules. Thos crazy Italialians use a decimal point as separator. – Jamiec Mar 15 '13 at 15:20
  • @user1929959: I don't think you meant that link... – Jon Skeet Mar 15 '13 at 15:28

2 Answers2

4

The dateTimePicker give me an European Format

That could only be true if you're using the Text property. I suggest you use the SelectedDate property, which will give you a DateTime? instead - and a DateTime doesn't have a format... it's up to you to format it however you want.

Then to convert them to US short date format, you should use ToString and specify d (for short date format) and the culture at the same time:

string str = m_dDate.Date.ToString("d", m_UsCulture);

Your current code is using DateTime.ToShortDateString() which will use the current culture, and then calling ToString on the resulting string which won't care about the culture you pass it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • OP is using the date: `m_dDate.Date` though converting to a string with `ToShortDateString` for some reason. – Oded Mar 15 '13 at 15:20
  • 1
    @Oded: It's not clear what `m_dDate` is here, to be honest. But my answer now shows how to convert a `DateTime` to a short date string in a specific culture. – Jon Skeet Mar 15 '13 at 15:21
  • Was commenting on the original version. And wasn't my downvote, btw. – Oded Mar 15 '13 at 15:22
  • @Oded: Sure. But fundamentally the OP's claim of "The dateTimePicker give me an European Format" indicates *either* the use of an inappropriate property, *or* a misunderstanding of DateTime. I wanted to try to correct that at the same time as providing the appropriate conversion. – Jon Skeet Mar 15 '13 at 15:24
4

ToShortDateString uses the current culture, so you can't specify your US culture with this method (unless you change the current culture). I would use ToString instead, and the US-specific short date pattern:

CultureInfo usCulture = CultureInfo.GetCultureInfo("en-us");
var stringRep = yourDate.ToString(
    usCulture.DateTimeFormat.ShortDatePattern, 
    usCulture);
ken2k
  • 48,145
  • 10
  • 116
  • 176