简体   繁体   中英

Convert(change) current DateTime as per culture in c#

if (!IsPostBack && !Page.IsCallback)
{
    double OffsetHrs = GetTimeZoneOffsetFromCookie();
    string dateFormat = ServiceManager.LocalizationService.GetString("AppHeaderTop", "DateFormat", "g");
    CultureSelected CultureSelected = GetCultureSelected();
    ASPxLabelCurrentTime.Text = DateTime.Now.ToUniversalTime().AddHours(-OffsetHrs).ToString(dateFormat);                
if (CultureSelected.CultureCode != "en-US") 
{
    DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;
    DateTimeFormatInfo currentDtfi = new CultureInfo(CultureSelected.CultureCode, false).DateTimeFormat;
    ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi).ToString(currentDtfi.ShortDatePattern); //what can i Use here ?
}               

Let say Output of ASPxLabelCurrentTime.Text for en-US culture is 11/2/2015 4:14 PM (70) If I select specific culture I want this datetime 11/2/2015 4:14 PM (70) to appear in that specific culture format.

Your question seems unclear but I try to give a shot.

First of all, what is this (70) exactly? Where is this came from? en-US culture can't parse this string without using it in a string literal delimiter with ParseExact or TryParseExact methods. On the other hand, since you assing ASPxLabelCurrentTime.Text the result of the DateTime.Now.ToUniversalTime().AddHours(-OffsetHrs).ToString(dateFormat) code, I don't believe this (70) part is really an issue on this question.

Second, If I understand clearly, the problem seems the usage of DateTime.ToString(string) method.

ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi)
                                   .ToString(currentDtfi.ShortDatePattern);
                                    // ^^^ Problem seems here

Okey let's say you successfully parse this ASPxLabelCurrentTime.Text with usDtfi culture (which is en-US ), but with this .ToString(string) method, you are not using currentDtfi settings actually, you are using CurrentCulture settings when you generate formatted string representation of your DateTime .

From DateTime.ToString(String) doc ;

Converts the value of the current DateTime object to its equivalent string representation using the specified format and the formatting conventions of the current culture .

Since we don't know what GetCultureSelected method returns exactly, it may or may not be the same culture with currentDtfi .

I strongly suspect, you can solve this problem to using that culture as a second parameter in ToString method as;

ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi)
                                   .ToString(currentDtfi.ShortDatePattern, currentDtfi);

IF this (70) is really part of on your string, you need to ParseExact or TryParseExact methods to supply exact format of it.

string s = "11/2/2015 4:14 PM (70)";
DateTime dt;
if(DateTime.TryParseExact(s, "MM/d/yyyy h:mm tt '(70)'", CultureInfo.GetCultureInfo("en-US"),
                          DateTimeStyles.None, out dt))
{
    ASPxLabelCurrentTime.Text = dt.ToString(currentDtfi.ShortDatePattern, currentDtfi);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM