Question

I am developing an app for Windows Phone. Due to different date formats used across the world, the datepicker in Windows Phone will display date as dd-MM-yyyy for users in India and MM/dd/yyyy for users in USA, just for an example. So my app crashes as it is set to use the MM/dd/yyyy format ONLY for a particular module.

Can you help me with some code snippet as I am unable to find a solution for this?

Edit: My try

DateTime dt = DateTime.ParseExact(x.msDate.ToString(),"M/d/yyyy",System.Globalization.CultureInfo.InvariantCulture);
Was it helpful?

Solution 4

A combination of all the answers did the trick for me. The successful implementation code:

    DateTime originalDate,dt;
    String msDate = "24-12-2013";
    String[] format = {"d-M-yyyy","M/d/yyyy"};
    CultureInfo enUS = new CultureInfo("en-US");
    foreach (var frmt in format)
    {
        if(DateTime.TryParseExact(msDate, frmt, enUS, DateTimeStyles.None,out dt))
        {
            originalDate = dt;
            Console.WriteLine(originalDate.ToString());
        }
     }

OTHER TIPS

Use the current culture of the machine running the code:

MyDateTime.ToString(CultureInfo.CurrentCulture)

Or the current culture of the machine running the UI code:

MyDateTime.ToString(CultureInfo.CurrentUICulture)

Or pass in a specific culture:

MyDateTime.ToString(new CultureInfo("en-US"))

Your example specifies an invariant culture, so it would expect the default DateTime string formatting. http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

If you can get your input to pass the DateTime in UTC, that will also make your life a lot easier.

Parsing string date like you did is dangerous. You should use the DateTime constructor that takes year, month,and day, in that way you will be safe

DateTime date1 = new DateTime(2010, 8, 18);

Or

So you may use TryParseExact http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact(v=vs.110).aspx

As read from your comments, x.msDate is a string that is stored in a database. If this string can be formatted for different cultures and you need a bit flexibility when parsing, you can use the TryParse method. There is an overload that takes a IFormatProvider as an input. If you want to parse that contains a DateTime in a format that differs from the format of the current culture, you can create a CultureInfo and use its DateTimeFormat property when parsing the DateTime:

DateTime dt;
if (!DateTime.TryParse(stringValue, out dt)
{
    if (!DateTime.TryParse(stringValue, new CultureInfo("en-US").DateTimeFormat, DateTimeStyles.None, out dt)
        throw new ArgumentException("Unable to parse date");
}
// If you reach this line, you were able to parse the DateTime.

Also you could create a a list of cultures and try to parse the string with the various culture settings. Once the string was parsed successfully, you return the value:

public DateTime ParseString(string value)
{
    CultureInfo[] cultures = {CultureInfo.CurrentCulture, 
                              new CultureInfo("en-US"), 
                              new CultureInfo("de-DE")};

    foreach(var cult in cultures)
    {
        DateTime dt;
        if (DateTime.TryParse(value, cult, DateTimeStyles.None, out dt))
            return dt;
    }
    throw new ArgumentException(
        string.Format("Unable to parse DateTime for string {0}.", value));
}

May this will help you

string dateTime= Convert.ToDateTime(datepicker.Value).ToString("M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top