0

I' like to return this short date in the US format, the current culture of my application is en-GB, but I'd like it in en-US.

Here's the code I have so far:

DateTime.Now.AddYears(-2).ToShortDateString();
Luke
  • 22,826
  • 31
  • 110
  • 193

5 Answers5

4

You need to ensure that you are using the right culture format string for this to happen.

One way to get this format directly from the culture is:

CultureInfo.GetCultureInfo("en-US").DateTimeFormat.ShortDatePattern

For me this returns M/d/yyyy.

var usShortDatePattern = 
             CultureInfo.GetCultureInfo("en-US").DateTimeFormat.ShortDatePattern;
DateTime.Now.AddYears(-2).ToString(usShortDatePattern);

The benefit of doing this is that if a user has overridden the Short Date Pattern for en-US in their control panel (Region and Language), they will get the formatting they want.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Taken from the documentation:

DateTime.Now.AddYears(-2).ToString("d", new CultureInfo("en-US"));
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 2
    Not sure it's a good idea to change the current thread culture in order to call a ToXXString() methods. There are DateTime method overloads that take a IFormatProvider as parameter (see http://stackoverflow.com/a/12003141/870604) – ken2k Aug 17 '12 at 09:25
  • @ken2k: That's one way to do it and is documented in the MSDN. I agree, I would rather use your method, now that I know of it :-) Still, **it is not wrong and does not deserve a downvote!** – Daniel Hilgarth Aug 17 '12 at 09:26
  • Just to clarify, I didn't downvoted your answer as it's of course not wrong :) – ken2k Aug 17 '12 at 09:28
  • @ken2k: I didn't think so, this sentence was meant for the anonymous downvoter. BTW: I found something even shorter, have a look :-) – Daniel Hilgarth Aug 17 '12 at 09:31
  • The documentation is to explain how `ToShortDateString()` works, for which it needs to change the current locale. It's not appropriate for real code as it could cause a mysterious bug to appear elsewhere in the same application (all of a sudden code in a completely different place that was correctly handling "12,56" for users where that is appropriate as a representation of 12.56 starts throwing exceptions) – Jon Hanna Aug 17 '12 at 09:41
  • @JonHanna: (1) I removed that part from my answer alltogether already (2) What you say is not true, as the code was setting the current culture *of the current thread* right before the call to `ToShortDateString` and setting it back to the original culture right afterwards. It is impossible to have effect on any other code. It can't affect threads running in parallel as I am changing the culture only for `CurrentThread`. It can't affect following code in the same thread as I set the culture back after my code finishes. Your downvote is not valid, so please remove it. – Daniel Hilgarth Aug 17 '12 at 09:43
  • You're assuming that everything always goes perfectly with the code. That's a brittle assumption. You're also assuming I downvoted you, that's a plain-wrong assumption. – Jon Hanna Aug 17 '12 at 09:50
  • @JonHanna: You could always put the part that sets the culture back in a finally block if that's what concerns you... – Daniel Hilgarth Aug 17 '12 at 09:51
  • You could. It'd still be strange for a one-off format specification but it wouldn't be as brittle. I do think your first form - but without the switch back - is worth leaving in as "if what you really want is to be using "en-US" in the first place...", as is "if you really want a custom culture with en-US date formats...", since they're likely scenarios where someone might be thinking "how do I get it American-style?" in the first place (whether the OP or someone else hitting this question). – Jon Hanna Aug 17 '12 at 10:03
1

Use .ToString() extension method and enter the format:

DateTime.Now.AddYears(-2).ToString("M/d/yyyy");

Custom date format options are documented here:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • 1
    How is this specific to the en-US culture the OP wants? – ken2k Aug 17 '12 at 09:29
  • @ken2k Because this is in the `en-US` format (month/day/year). Why do you consider this wrong? – Curtis Aug 17 '12 at 09:33
  • You sure about that? On my machine it's M/d/yyyy. – ken2k Aug 17 '12 at 09:34
  • @ken2k actually, we don't know if the OP means "en-US" as understood by .NET or "en-US" as an identifier used in lots of systems for English-language-as-used-in-the-US locale information, so Curt could be closer to what they really want (hence my giving both approaches). – Jon Hanna Aug 17 '12 at 09:38
  • @ken2k So you're saying `M/d/yyyy` is en-US format but `MM/dd/yyyy` isn't? These are both `month/day/year` formats, its just `MM/dd/yyyy` will display month and date as 2 digits. – Curtis Aug 17 '12 at 09:38
  • @Curt Yes, so basically those are different patterns. The en-US (as per .Net definition) format has only one digit. – ken2k Aug 17 '12 at 09:52
  • @ken2k I've seen both formats documentated on msdn, but for arguments sake I've changed the format – Curtis Aug 17 '12 at 09:58
  • Yes, but now if they change their locale data again, it's wrong in the other direction. Why not just ask .NET what that format is? – Jon Hanna Aug 17 '12 at 10:05
1
var culture = new CultureInfo("en-us");
string formatedDate = DateTime
    .Now
    .AddYears(-2)
    .ToString(culture.DateTimeFormat.ShortDatePattern, culture);
ken2k
  • 48,145
  • 10
  • 116
  • 176
1

"I want to apply the en-US short-date-pattern":

DateTime.Now.AddYears(-2).ToString("d", CultureInfo.GetCultureInfo("en-US"));

"I want to apply the pattern M/d/yyyy regardless of whether .NET thinks that's the short-pattern for en-US or not:

DateTime.Now.AddYears(-2).ToString(@"M\/d\/yyyy", CultureInfo.InvariantCulture)

(Same result as above, but if what you are thinking of as "the US format" was actually MM/dd/yyyy, which is also used in the US, then that's the approach you want, but with @"MM\/dd\/yyyy"instead of @"M\/d\/yyyy".

Finally, "I want to find out the en-US short-date-pattern for use with another call:

CultureInfo.GetCultureInfo("en-US").DateTimeFormat.ShortDatePattern
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251