Search code examples
c#.netdatetimecultureinfo

Get Utc datetime in specific culture independent of my current UI culture


If my application is in Arabic culture then DateTime.UtcNow gives us arabic clander datetime like "13/05/1435 09:40:45 ص" but i need it in en-US what ever culture is used by my user.

When i am enforce to convert this date to us culture it gives me an error. what is the better way to get en-US datetime even in arabic culture like this "14/03/2014 09:40:45 AM".

var us = DateTime.Now.ToString("dd/MM/yy HH:mm:ss tt",
            new System.Globalization.CultureInfo("en-US"));

            string st = us.ToString();

            DateTime ddt = Convert.ToDateTime(st,
            new System.Globalization.CultureInfo("en-US"));

When converting from st to DateTime Again in en-US When Application is in Arabic Culture it throw error. enter image description here

Mr @lastr2d2

Thanks in advance.


Solution

  • Is this what you excepted?

    var ar = DateTime.Now.ToString("dd/MM/yy HH:mm:ss tt",
                new System.Globalization.CultureInfo("ar-AE"));
    // 14/03/14 06:14:34 ص
    var us = DateTime.Now.ToString("dd/MM/yy HH:mm:ss tt",
                new System.Globalization.CultureInfo("en-US"));
    // 14/03/14 06:14:44 AM
    

    --Edit

    And you can get the datetime instance by DateTime.ParseExact :

    var datetime = DateTime.ParseExact(us,"dd/MM/yy HH:mm:ss tt",
                new System.Globalization.CultureInfo("en-US"));