C# DateTime ToString 标准区域性格式
C# DateTime ToString Standard culture format
我可以更改特定文化的 DateTime 的标准输出格式吗?示例:
class Program
{
static void Main(string[] args)
{
PrintCultureDateTime("ca-ES");
PrintCultureDateTime("es-ES");
PrintCultureDateTime("en-US");
PrintCultureDateTime("en-GB");
PrintCultureDateTime("pt-PT");
}
public static void PrintCultureDateTime(string culture)
{
string result = new DateTime(2017, 10, 1).ToString("d",
CultureInfo.GetCultureInfo(culture));
Console.WriteLine(string.Format("{0}: {1}", culture, result));
}
}
输出:
ca-ES: 1/10/2017
es-ES: 01/10/2017
en-US: 10/1/2017
en-GB: 01/10/2017
pt-PT: 01/10/2017
我想要的是使用标准 "d" 格式或其他标准格式使用两位数字表示日期和月份,以固定 DateTime 字符串的大小。类似这样的输出:
ca-ES: 01/10/2017
es-ES: 01/10/2017
en-US: 10/01/2017
en-GB: 01/10/2017
pt-PT: 01/10/2017
你可以使用DateTimeFormatInfo.ShortDatePattern
来获取模式,然后如果你真的想用dd
替换d
。您可能需要考虑转义或引用 d
模式等极端情况 - 以及显然不将 dd
替换为 dddd
。 (请注意,来自系统文化的格式信息将是 read-only;克隆该文化以获得具有 read/write 格式信息的文化。)当然,您也会在几个月内做同样的事情。您甚至可能需要更改年份部分。
我不清楚这样做是否是个好主意 - 当您更改格式时,它不再是 "the standard short date format for the culture"。
即便如此,也不能保证所有文化的长度都相同。没有什么可以阻止文化使用 "'Year:' yyyy '; Month: ' MM '; 'Day: 'dd"
的短日期格式。这将是非常不寻常的,但并非无效。
像这样的方法可能适用于大多数日期格式,但正如 Jon Skeet 所说,可能会有一些难以处理的奇怪格式。
public class CustomFormatProvider : IFormatProvider
{
private string _culture;
public CustomFormatProvider(string culture)
{
_culture = culture;
}
public object GetFormat(Type formatType)
{
if (formatType.Equals(typeof(DateTimeFormatInfo)))
{
var format =
CultureInfo.GetCultureInfo(_culture).DateTimeFormat;
var info = new DateTimeFormatInfo();
switch (format.ShortDatePattern.ToString())
{
case "d/M/yyyy":
case "d/MM/yyyy":
case "dd/M/yyyy":
case "dd/MM/yyyy":
info.ShortDatePattern = "dd/MM/yyyy";
return info;
case "M/dd/yyyy":
case "MM/dd/yyyy":
case "M/d/yyyy":
case "MM/d/yyyy":
info.ShortDatePattern = "MM/dd/yyyy";
return info;
default:
//This is just example for default handling
info.ShortDatePattern = "dd/MM/yyyy";
return info;
}
}
else return null;
}
}
用法:
public static void PrintCultureDateTime(string culture)
{
string result = new DateTime(2017, 10, 1).ToString("d", new
CustomFormatProvider(culture));
Console.WriteLine(string.Format("{0}: {1}", culture, result));
}
输出:
基于@Jon Skeet 的回答,我实施了以下解决方案。我认为必须存在一个更好的单次迭代正则表达式替换指令,但我没有更多时间找到它。
它不适用于格式“'Year:' yyyy ';月:'MM';'日:'dd" but I' 假设 "error"。
public static void PrintCultureDateTime(string culture)
{
string result = new DateTime(2017, 10, 1).ToString(FixedSizeShortDatePattern(culture));
Console.WriteLine(string.Format("{0}: {1}", culture, result));
}
public static string FixedSizeShortDatePattern(string culture)
{
var cultureInfo = CultureInfo.GetCultureInfo(culture);
string format = cultureInfo.DateTimeFormat.ShortDatePattern;
return ReplaceSingleChar(ReplaceSingleChar(format, 'd'), 'M');
}
public static string ReplaceSingleChar(string input, char c)
{
string cStr = c.ToString();
string strRegex = string.Format(@"^({0})[^{0}]|[^{0}]({0})[^{0}]|[^{0}]({0})$", cStr);
return Regex.Replace(input, strRegex, (match) =>
{
string token = match.Groups[0].Value;
if (!string.IsNullOrEmpty(token))
{
return match.Value.Replace(cStr, string.Concat(cStr, cStr));
}
return token;
});
}
我可以更改特定文化的 DateTime 的标准输出格式吗?示例:
class Program
{
static void Main(string[] args)
{
PrintCultureDateTime("ca-ES");
PrintCultureDateTime("es-ES");
PrintCultureDateTime("en-US");
PrintCultureDateTime("en-GB");
PrintCultureDateTime("pt-PT");
}
public static void PrintCultureDateTime(string culture)
{
string result = new DateTime(2017, 10, 1).ToString("d",
CultureInfo.GetCultureInfo(culture));
Console.WriteLine(string.Format("{0}: {1}", culture, result));
}
}
输出:
ca-ES: 1/10/2017
es-ES: 01/10/2017
en-US: 10/1/2017
en-GB: 01/10/2017
pt-PT: 01/10/2017
我想要的是使用标准 "d" 格式或其他标准格式使用两位数字表示日期和月份,以固定 DateTime 字符串的大小。类似这样的输出:
ca-ES: 01/10/2017
es-ES: 01/10/2017
en-US: 10/01/2017
en-GB: 01/10/2017
pt-PT: 01/10/2017
你可以使用DateTimeFormatInfo.ShortDatePattern
来获取模式,然后如果你真的想用dd
替换d
。您可能需要考虑转义或引用 d
模式等极端情况 - 以及显然不将 dd
替换为 dddd
。 (请注意,来自系统文化的格式信息将是 read-only;克隆该文化以获得具有 read/write 格式信息的文化。)当然,您也会在几个月内做同样的事情。您甚至可能需要更改年份部分。
我不清楚这样做是否是个好主意 - 当您更改格式时,它不再是 "the standard short date format for the culture"。
即便如此,也不能保证所有文化的长度都相同。没有什么可以阻止文化使用 "'Year:' yyyy '; Month: ' MM '; 'Day: 'dd"
的短日期格式。这将是非常不寻常的,但并非无效。
像这样的方法可能适用于大多数日期格式,但正如 Jon Skeet 所说,可能会有一些难以处理的奇怪格式。
public class CustomFormatProvider : IFormatProvider
{
private string _culture;
public CustomFormatProvider(string culture)
{
_culture = culture;
}
public object GetFormat(Type formatType)
{
if (formatType.Equals(typeof(DateTimeFormatInfo)))
{
var format =
CultureInfo.GetCultureInfo(_culture).DateTimeFormat;
var info = new DateTimeFormatInfo();
switch (format.ShortDatePattern.ToString())
{
case "d/M/yyyy":
case "d/MM/yyyy":
case "dd/M/yyyy":
case "dd/MM/yyyy":
info.ShortDatePattern = "dd/MM/yyyy";
return info;
case "M/dd/yyyy":
case "MM/dd/yyyy":
case "M/d/yyyy":
case "MM/d/yyyy":
info.ShortDatePattern = "MM/dd/yyyy";
return info;
default:
//This is just example for default handling
info.ShortDatePattern = "dd/MM/yyyy";
return info;
}
}
else return null;
}
}
用法:
public static void PrintCultureDateTime(string culture)
{
string result = new DateTime(2017, 10, 1).ToString("d", new
CustomFormatProvider(culture));
Console.WriteLine(string.Format("{0}: {1}", culture, result));
}
输出:
基于@Jon Skeet 的回答,我实施了以下解决方案。我认为必须存在一个更好的单次迭代正则表达式替换指令,但我没有更多时间找到它。
它不适用于格式“'Year:' yyyy ';月:'MM';'日:'dd" but I' 假设 "error"。
public static void PrintCultureDateTime(string culture)
{
string result = new DateTime(2017, 10, 1).ToString(FixedSizeShortDatePattern(culture));
Console.WriteLine(string.Format("{0}: {1}", culture, result));
}
public static string FixedSizeShortDatePattern(string culture)
{
var cultureInfo = CultureInfo.GetCultureInfo(culture);
string format = cultureInfo.DateTimeFormat.ShortDatePattern;
return ReplaceSingleChar(ReplaceSingleChar(format, 'd'), 'M');
}
public static string ReplaceSingleChar(string input, char c)
{
string cStr = c.ToString();
string strRegex = string.Format(@"^({0})[^{0}]|[^{0}]({0})[^{0}]|[^{0}]({0})$", cStr);
return Regex.Replace(input, strRegex, (match) =>
{
string token = match.Groups[0].Value;
if (!string.IsNullOrEmpty(token))
{
return match.Value.Replace(cStr, string.Concat(cStr, cStr));
}
return token;
});
}