仅使用日期时间格式获取小写日期时间的 AM/PM
-
20-08-2019 - |
题
我要获取自定义日期时间格式,包括 AM/PM 指示符,但我希望“AM”或“PM”小写 没有 将其余字符设为小写。
是否可以使用单一格式而不使用正则表达式?
这是我现在所拥有的:
item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt")
现在的输出示例是 2009 年 1 月 31 日星期六下午 1:34
解决方案
我个人格式化在两个部分:非上午/下午部分,而AM / PM部与ToLower将:
string formatted = item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mm") +
item.PostedOn.ToString("tt").ToLower();
另一种选择(我将在仲调查)是抓住当前的DateTimeFormatInfo,创建一个副本,并设置上午/下午指示符到下壳体的版本。然后使用正常格式化该格式的信息。你想要缓存的DateTimeFormatInfo,显然...
编辑:尽管我的意见,我写缓存位反正。它可能不会是的更快的比上面的代码(因为它涉及锁和字典查找),但它确实让调用代码简单:
string formatted = item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt",
GetLowerCaseInfo());
下面是一个完整的程序来演示:
using System;
using System.Collections.Generic;
using System.Globalization;
public class Test
{
static void Main()
{
Console.WriteLine(DateTime.Now.ToString("dddd, MMMM d, yyyy a\\t h:mmtt",
GetLowerCaseInfo());
}
private static readonly Dictionary<DateTimeFormatInfo,DateTimeFormatInfo> cache =
new Dictionary<DateTimeFormatInfo,DateTimeFormatInfo>();
private static object cacheLock = new object();
public static DateTimeFormatInfo GetLowerCaseInfo()
{
DateTimeFormatInfo current = CultureInfo.CurrentCulture.DateTimeFormat;
lock (cacheLock)
{
DateTimeFormatInfo ret;
if (!cache.TryGetValue(current, out ret))
{
ret = (DateTimeFormatInfo) current.Clone();
ret.AMDesignator = ret.AMDesignator.ToLower();
ret.PMDesignator = ret.PMDesignator.ToLower();
cache[current] = ret;
}
return ret;
}
}
}
其他提示
您可以在格式字符串分割成两个部分,然后小写AM / PM部分,像这样:
DateTime now = DateTime.Now;
string nowString = now.ToString("dddd, MMMM d, yyyy a\\t h:mm");
nowString = nowString + now.ToString("tt").ToLower();
然而,我认为更好的解决方案是使用一个 DateTimeFormatInfo
实例的你构建和更换 AMDesignator
和 PMDesignator
与性质“是”和 “分别PM”:
DateTimeFormatInfo fi = new DateTimeFormatInfo();
fi.AMDesignator = "am";
fi.PMDesignator = "pm";
string nowString = now.ToString("dddd, MMMM d, yyyy a\\t h:mmtt", fi);
您可以使用DateTimeFormatInfo
实例来定制转化DateTime
到string
的许多其他方面。
编辑:乔恩的示例要好得多,尽管我认为扩展方法仍然是可行的方法,因此您不必到处重复代码。我已经删除了替换并替换了扩展方法中 Jon 的第一个示例。我的应用程序通常是 Intranet 应用程序,我不必担心非美国文化。
添加一个扩展方法来为您执行此操作。
public static class DateTimeExtensions
{
public static string MyDateFormat( this DateTime dateTime )
{
return dateTime.ToString("dddd, MMMM d, yyyy a\\t h:mm") +
dateTime.ToString("tt").ToLower();
}
}
...
item.PostedOn.MyDateFormat();
编辑:有关如何执行此操作的其他想法,请访问 如何格式化日期时间,例如“Oct.2008 年 10 月 10 日上午 10:43 CST”(C# 语言).
与上述方法的问题是主要的原因,你使用格式字符串是使本地化,到目前为止给出的方法将打破任何国家或文化不希望包括最后的上午或下午。所以我所做的就是写出一个理解的附加格式序列“TT”,这标志着一个小写的上午/下午的扩展方法。下面的代码调试了我的情况,但可能还不是完美的:
/// <summary>
/// Converts the value of the current System.DateTime object to its equivalent string representation using the specified format. The format has extensions over C#s ordinary format string
/// </summary>
/// <param name="dt">this DateTime object</param>
/// <param name="formatex">A DateTime format string, with special new abilities, such as TT being a lowercase version of 'tt'</param>
/// <returns>A string representation of value of the current System.DateTime object as specified by format.</returns>
public static string ToStringEx(this DateTime dt, string formatex)
{
string ret;
if (!String.IsNullOrEmpty(formatex))
{
ret = "";
string[] formatParts = formatex.Split(new[] { "TT" }, StringSplitOptions.None);
for (int i = 0; i < formatParts.Length; i++)
{
if (i > 0)
{
//since 'TT' is being used as the seperator sequence, insert lowercase AM or PM as appropriate
ret += dt.ToString("tt").ToLower();
}
string formatPart = formatParts[i];
if (!String.IsNullOrEmpty(formatPart))
{
ret += dt.ToString(formatPart);
}
}
}
else
{
ret = dt.ToString(formatex);
}
return ret;
}
这应该是最高效的所有这些选项。但太糟糕了,他们不能在一个小写的选择到DateTime格式工作(TT相反TT?)。
public static string AmPm(this DateTime dt, bool lower = true)
{
return dt.Hour < 12
? (lower ? "am" : "AM")
: (lower ? "pm" : "PM");
}