我有一个DateTime对象,我试图以ISO 8601兼容格式输出到xml文件中,以便在两个系统之间进行传输 - 我们无法控制收件人。 .net round trip format在很大程度上满足了这个要求,但是将精度强制为7dp。
有没有办法可以指明这个?例如像"o:0"
这样的东西可以完全省略毫秒小数位,或者"o:3"
来将其设置为3dp。我知道我可以使用yyyy'-'MM'-'dd'T'HH':'mm':'ssK
(或分别为yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK
)自己格式化输出作为自定义格式说明符,以便在必要时控制小数位,但是想知道我是否错过了简单传递小数的方法精确到"o"
格式说明符。
答案 0 :(得分:1)
似乎答案是否定的,没有任何东西(至少在核心框架中)来定制它。查看source code for DateTime
roundtrip formatting这是硬编码为7dp:
// Omitted for brevity
...
AppendHHmmssTimeOfDay(result, dateTime);
result.Append('.');
long fraction = dateTime.Ticks % TimeSpan.TicksPerSecond;
AppendNumber(result, fraction, 7);
如果有人感兴趣,我的解决方案是实现一个自定义的ToFormattedString()
扩展方法来处理这个问题,如果需要的话,它将用自定义格式字符串替换格式字符串,并用{:}调用ToString
>
using System.Globalization; // System.Globalization needed for IFormatProvider overload
using System.Text; // System.Text required for StringBuilder class
namespace Extensions
{
public static class DateTimeFormatExtension
{
// Consts for building the custom format string
private const string ROUNDTRIP_FORMAT_PREFIX = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
private const char ROUNDTRIP_FORMAT_FRACTION = 'f';
private const char ROUNDTRIP_FORMAT_SUFFIX = 'K';
// Appending the 'f' custom format string maxes out at "fffffff"(7 dp) and will throw an exception if given more
private const int DATETIME_MAX_DP = 7;
private static int GetRoundtripLength(int decimalPlaces) =>
ROUNDTRIP_FORMAT_PREFIX.Length + decimalPlaces + 2; // +2 to account for the '.' and the 'K' suffix
public static string ToFormattedString(this DateTime input) => input.ToString();
public static string ToFormattedString(this DateTime input, string format)
{
var provider = DateTimeFormatInfo.CurrentInfo;
return input.ToFormattedString(format, provider);
}
public static string ToFormattedString(this DateTime input, string format, IFormatProvider provider)
{
string parsedFormat = format;
if (!string.IsNullOrWhiteSpace(format))
{
switch (format[0])
{
case 'o':
case 'O':
var precision = format.Substring(1);
// Only do this if we have a custom 'o' string, otherwise us the base functionality
if (!string.IsNullOrWhiteSpace(precision))
{
// If the custom addition to the format string is an integer, use that to determine dp
if (int.TryParse(precision, out int decimalCount))
{
// Build the format string
var formatBuilder = new StringBuilder(GetRoundtripLength(decimalCount));
formatBuilder.Append(ROUNDTRIP_FORMAT_PREFIX);
// Append '.' and 'f' chars to format string (Append nothing if 0 dp)
if (decimalCount > 0)
{
formatBuilder
.Append('.')
.Append(ROUNDTRIP_FORMAT_FRACTION,
// Cap max dp length to avoid exceptions
Math.Min(decimalCount, DATETIME_MAX_DP));
}
// Append 'K' suffix
formatBuilder.Append(ROUNDTRIP_FORMAT_SUFFIX);
parsedFormat = formatBuilder.ToString();
}
}
break;
default:
break;
}
}
return input.ToString(parsedFormat, provider);
}
}
}
然后可以这样使用:
using Extensions;
...
DateTime currentDateTime = DateTime.UtcNow;
string result;
result = currentDateTime.ToFormattedString(); // empty just calls the default ToString()
// result: "2018-03-02 12:31:17 AM"
result = currentDateTime.ToFormattedString("dd/MM/yy ssmmhh"); // custom format strings still work
// result: "02-03-18 173112"
result = currentDateTime.ToFormattedString("d"); // standard format strings still work
// result: "2018-03-02"
result = currentDateTime.ToFormattedString("D");
// result: "Friday, March 2, 2018"
result = currentDateTime.ToFormattedString("F");
// result: "Friday, March 2, 2018 12:31:17 AM"
result = currentDateTime.ToFormattedString("o"); // standard format specifier uses default ToString("o") behaviour
// result: "2018-03-02T00:31:17.9818727Z"
result = currentDateTime.ToFormattedString("o0"); // no decimal places
// result: "2018-03-02T00:31:17Z"
result = currentDateTime.ToFormattedString("o3"); // 3 decimal places
// result: "2018-03-02T00:31:17.981Z"
result = currentDateTime.ToFormattedString("o100"); // too many decimals cap at 7
// result: "2018-03-02T00:31:17.9818727Z"
编辑:已更新,删除":"从格式字符串开始,以符合numeric formatting上指定的小数精度的方式:
标准数字格式字符串采用
Axx
形式,其中:
A
是一个称为格式说明符的单个字母字符。
...
xx
是一个可选的整数,称为精度说明符