获取分隔符
正如Jeppe Stig Nielson所说(也许可以给他点赞),没有一个好方法可以得到第二个时间或日期的分隔符,因为在格式字符串中
HH-mm/HH:mm-HH/mm
它们可以有多个,甚至具有相同的语义(例如在小时和分钟之间)。
微软错误报告
我已经在Microsoft Connect上注册,并将错误报告为DateTime.ToString("T") and DateTime.ToString("G")。如果您有Microsoft Connect帐户,则可以投票是否可以重现该错误。
可重现该错误的SSCCE
using System;
using System.Globalization;
namespace DateTimeToString
{
class Program
{
static void Main()
{
if (DateTimeFormatInfo.CurrentInfo.LongTimePattern != "HH-mm:ss" ||
DateTimeFormatInfo.CurrentInfo.ShortDatePattern != "dd.MM/yyyy")
{
Console.WriteLine("Configure long time format to MM-mm:ss to reproduce the time bug.");
Console.WriteLine("Configure short date format to dd.MM/yyyy to reproduce the date bug.");
Console.WriteLine("Control Panel/Region and Language/Additional settings");
return;
}
var dateTime = DateTime.Now;
Console.WriteLine("Expected: " + dateTime.ToString("HH'-'mm':'ss"));
Console.WriteLine("Actual : " + dateTime.ToString("T"));
Console.WriteLine();
Console.WriteLine("Expected: " + dateTime.ToString("dd'.'MM'/'yyyy HH'-'mm':'ss"));
Console.WriteLine("Actual : " + dateTime.ToString("G"));
Console.WriteLine();
Console.WriteLine("Expected: " + dateTime.ToString("HH'-'mm':'ss"));
Console.WriteLine("Actual : " + dateTime.ToLongTimeString());
Console.WriteLine();
Console.WriteLine("Expected: " + dateTime.ToString("dd'.'MM'/'yyyy"));
Console.WriteLine("Actual : " + dateTime.ToShortDateString());
Console.ReadLine();
}
}
}
解决方法
作为一种解决方法,我们可以使用本地方法GetTimeFormat和GetDateFormat。
static class Program
{
static void Main()
{
var systemTime = new SystemTime(DateTime.Now);
Console.WriteLine("ShortDatePattern (as reported by .NET): " + DateTimeFormatInfo.CurrentInfo.ShortDatePattern);
var sbDate = new StringBuilder();
GetDateFormat(0, 0, ref systemTime, null, sbDate, sbDate.Capacity);
Console.WriteLine("Date string (as reported by kernel32) : " + sbDate);
Console.WriteLine();
Console.WriteLine("LongTimePattern (as reported by .NET) : " + DateTimeFormatInfo.CurrentInfo.LongTimePattern);
var sbTime = new StringBuilder();
GetTimeFormat(0, 0, ref systemTime, null, sbTime, sbTime.Capacity);
Console.WriteLine("Time string (as reported by kernel32) : " + sbTime);
Console.ReadKey();
}
[DllImport("kernel32.dll")]
private static extern int GetDateFormat(int locale, uint dwFlags, ref SystemTime sysTime,
string lpFormat, StringBuilder lpDateStr, int cchDate);
[DllImport("kernel32.dll")]
private static extern int GetTimeFormat(uint locale, uint dwFlags, ref SystemTime time,
string format, StringBuilder sb, int sbSize);
[StructLayout(LayoutKind.Sequential)]
private struct SystemTime
{
[MarshalAs(UnmanagedType.U2)] private readonly ushort Year;
[MarshalAs(UnmanagedType.U2)] private readonly ushort Month;
[MarshalAs(UnmanagedType.U2)] private readonly ushort DayOfWeek;
[MarshalAs(UnmanagedType.U2)] private readonly ushort Day;
[MarshalAs(UnmanagedType.U2)] private readonly ushort Hour;
[MarshalAs(UnmanagedType.U2)] private readonly ushort Minute;
[MarshalAs(UnmanagedType.U2)] private readonly ushort Second;
[MarshalAs(UnmanagedType.U2)] private readonly ushort Milliseconds;
public SystemTime(DateTime dateTime)
{
Year = (ushort) dateTime.Year;
Month = (ushort) dateTime.Month;
DayOfWeek = (ushort) dateTime.DayOfWeek;
Day = (ushort) dateTime.Day;
Hour = (ushort) dateTime.Hour;
Minute = (ushort) dateTime.Minute;
Second = (ushort) dateTime.Second;
Milliseconds = (ushort) dateTime.Millisecond;
}
}
}
CultureInfo c2 = CultureInfo.CurrentCulture;
Console.WriteLine(c2.DateTimeFormat.ToString());
您将看到属性“LongTimePattern”。然后,也许您可以使用正则表达式检查并提取分隔符... - blfuentes