0

At the moment I'm returning the date like this:

string returnValue = DateTime.Now.ToString("d MMMMMMMMMMMMMM yyyy");

The problem is, if the person who is running the program is from the USA, I need to return it like this: October 15, 2014

When the user runs the program, I can change the cultureinfo with this code (this is working):

Thread.CurrentThread.CurrentCulture = new CultureInfo(SelectedLanguage.LanguageCode);

I can use

string returnValue = DateTime.Now.ToString("d MMMMMMMMMMMMMM yyyy");
if(Thread.CurrentThread.CurrentCulture == "en-US") {
    returnValue = DateTime.Now.ToString("MMMMMMMMMMMMMM d, yyyy");
}

But if there is another region, I need to add another if-statement... Can't I write this with dynamic code, so I have the correct format with one line of code?

endeka
  • 131
  • 1
  • 15
  • Are you looking for `DateTime.ToShortDateString()`? – Andrey Korneyev Oct 15 '14 at 13:36
  • If you can use the default format for a culture, this questin/answer will help http://stackoverflow.com/questions/1661325/simpledateformat-and-locale-based-format-string – Gus Oct 15 '14 at 13:36
  • Am I only one curious about what is `MMMMMMMMMMMMMM` format for exactly? Why not just `MMMM`? – Soner Gönül Oct 15 '14 at 13:57
  • @SonerGönül I need to edit some existing code that is written by another person. Don't know why he wrote `MMMMMMMMMMMMMM` – endeka Oct 16 '14 at 07:13

2 Answers2

1

If you can change your requirements slightly, you could use a standard format like "D" for all cultures (or another standard format that makes sense):

DateTime.Now.ToString("D"); // i.e., Monday, June 15, 2009

Otherwise you could use a Dictionary<CultureInfo, string> to create a lookup of culture/format pairs and then use that instead of a long if/else chain:

var formatLookup = new Dictionary<CultureInfo, string>();
formatLookup.Add(CultureInfo.GetCultureInfo("en-us"), "MMMMMMMMMMMMMM d, yyyy");

Then you could use it like this:

string format;

if (!formatLookup.TryGetValue(Thread.CurrentThread.CurrentCulture, out format))
{
    // This would be the default format if none is found for the current culture:
    format = "d MMMMMMMMMMMMMM yyyy";
}

string returnValue = DateTime.Now.ToString(format);

Another option would exist if you're using resource files to localize your application. If this is the case you could store the format for each language in a resource string instead. The ResourceManager would automatically pick up the correct format, assuming there's a localization file for the current culture.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
0

You can implement a simple culture provider/helper (let it be extension method) to centralize logic in one place

public static class CultureHelper
{
    public static string ToCultureSpecificString(this DateTime @this)
    {
        // do formatting as you like, check current country, user nationality, etc.
        // var result = @this.ToString("dd/MM/yyyy");
        return result;
    }
}

And use it for all DateTime's like this

string returnValue = DateTime.Now.ToCultureSpecificString();
Sinatr
  • 20,892
  • 15
  • 90
  • 319