开发者

asp.net | Time Zone | Format | CultureInfo

I need to convert datetime to en-us culture format ...

but our users are in different locality; some people follow

DD.MM.YYYY hh:mm:ss tt,

I'm getting an error

"String was not recognized as a valid DateTime"

while converting the

time zone to MM/DD/YYYY hh:mm:ss tt (en-us).

System.Globalization.CultureInfo oCulture = new System.Globalization.CultureInfo("en-US", false);

        public string CultureStringFormat(string Date)
        {
            DateTime _Datetime = DateTime.Now;
            try
            {
                _Datetime = DateTime.Parse(Date);   
                return _Datetime.ToString("G", oCulture);
            }
            catch (Exception ex)
            {
                try
                {
                    _Datetime = DateTime.Parse(Date, oCulture);
                    return _Datetime.ToString("G", oCulture);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            return "";
        }

        public DateTime CultureDateFormat(string Date)
        {
            try
            {               
               return DateTime.Parse(Date, oCulture, DateTimeStyles.NoCurrentDateDefault);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return DateTime.Now;
        }




    Thread.CurrentThread.CurrentCulture = oCulture;
    string sDate = CultureStringFormat("28.12.2011 10:15:07"); // i'm getting an Error
    DateTime dtDate = CultureDateFormat("28.12.2011 10:15:07"); // Error

    string sDate1 = CultureStringFormat("12.27.2011 10:15:07"); // i'm getting success  12/27/2011 10:15:07 AM
    DateTime dtDate1 = CultureDateFormat("12.27.2011 10:15:07"); //success  12/27/2011 10:15:07 AM



      Thread.CurrentThread.CurrentCulture = oCulture; //Without this line ...

    string sDate = CultureStringFormat("28.12.2011 10:15:07"); // success 12/28/201开发者_开发技巧1 10:15:07 AM
    DateTime dtDate = CultureDateFormat("28.12.2011 10:15:07"); // Error

    string sDate1 = CultureStringFormat("12.27.2011 10:15:07");// success 12/27/2011 10:15:07 AM
    DateTime dtDate1 = CultureDateFormat("12.27.2011 10:15:07");// Wrong 27.12.2011 10:15:07


You need to use DateTime.TryParseExact. The snippet below should get you started in the right direction:

string myDate = "17.11.2011 08:00:00 AM";
DateTime parsedDate;

DateTime.TryParseExact(myDate,
                       "dd.MM.yyyy hh:mm:ss tt",
                       CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None,
                       out parsedDate);


public DateTime? GetDate(string dateString)
{
    var formats = new string[] 
    {
        "dd.MM.yyyy hh:mm:ss",
        "MM/dd/yyyy hh:mm:ss",
        "dd.MM.yyyy HH:mm:ss",
        "MM/dd/yyyy HH:mm:ss",
        "dd.MM.yyyy hh:mm:ss tt",
        "MM/dd/yyyy hh:mm:ss tt"        
    };
    DateTime date;
    if ( DateTime.TryParseExact(dateString, formats, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out date))
    {
        return date;
    }   
    return null;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜