[英]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. 假设针对
en-US
文化的ASPxLabelCurrentTime.Text
输出为ASPxLabelCurrentTime.Text
11/2/2015 4:14 PM (70)
如果选择特定的文化,我希望此日期时间为11/2/2015 4:14 PM (70)
在其中显示具体的文化格式。
Your question seems unclear but I try to give a shot. 您的问题似乎不清楚,但我尝试尝试一下。
First of all, what is this (70)
exactly? 首先,这
(70)
到底是什么? 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. 在不使用带有
ParseExact
或TryParseExact
方法的字符串文字定界符中的情况下, en-US
文化无法解析此字符串。 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. 另一方面,由于您将
ASPxLabelCurrentTime.Text
设置为DateTime.Now.ToUniversalTime().AddHours(-OffsetHrs).ToString(dateFormat)
代码的结果,因此我不认为这(70)
部分确实是一个问题这个问题。
Second, If I understand clearly, the problem seems the usage of DateTime.ToString(string)
method. 其次,如果我理解清楚,那么问题似乎出在
DateTime.ToString(string)
方法的使用上。
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
. Okey假设您使用
usDtfi
区域性( en-US
)成功解析了此ASPxLabelCurrentTime.Text
, 但是使用此.ToString(string)
方法,您实际上并没有使用currentDtfi
设置,而是在生成格式化的字符串表示形式时使用了CurrentCulture
设置。您的DateTime
。
From DateTime.ToString(String)
doc ; 从
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 .
使用指定的格式和当前区域性的格式约定将当前DateTime对象的值转换为其等效的字符串表示形式。
Since we don't know what GetCultureSelected
method returns exactly, it may or may not be the same culture with currentDtfi
. 因为我们不知道什么
GetCultureSelected
方法正好返回,它可能会或可能不会是相同的文化与currentDtfi
。
I strongly suspect, you can solve this problem to using that culture as a second parameter in ToString
method as; 我强烈怀疑,您可以通过在
ToString
方法中使用该区域性作为第二个参数来解决此问题;
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. 如果此
(70)
实际上是字符串的一部分,则需要使用ParseExact
或TryParseExact
方法来提供其确切格式。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.