【发布时间】:2013-08-26 04:19:36
【问题描述】:
我知道如何将DateTime 格式化为String。可能有很多方法可以将DateTime 格式化为String,我发现与DateTime 格式化相关的问题很少。
但我找到并尝试了两种方法:
GetDateTimeFormats()ToString("MM/dd/yyyy")
我试过这段代码:
public static class DateTimeFormates
{
public static string FormatMyDate(this DateTime DT)
{
// My Actual Date Time Comes in this Format
//8/23/2013 12:43:12 PM
// I'm expecting Format like below
String FormatWayOne = DT.GetDateTimeFormats()[3];
//08/23/2013
String FormatWayTwo = DT.ToString("MM/dd/yyyy");
//08/23/2013
return FormatWayOne;
}
}
我有一个带有Global 方法的Global 类将DateTime 格式化为String。但是在这里我想知道哪种方法是最佳实践以便我可以使用它?该方法在整个应用程序中被调用。
【问题讨论】:
-
仅供参考
DateTime没有任何格式。它存储为Long(Ticks)。DateTime的字符串表示只拥有格式 -
+1 奇怪,没听说过
DateTime.GetDateTimeFormats。
标签: c# .net datetime formatting string-formatting