【问题标题】:DateTime string conversion with both month name and AM/PM带有月份名称和 AM/PM 的 DateTime 字符串转换
【发布时间】:2016-09-23 02:51:40
【问题描述】:

我查看了一堆关于将字符串解析为 DateTime 对象的问题,但似乎没有一个问题与我的格式相同。我需要解析为 DateTime 对象的字符串采用以下格式:

2016 年 6 月 29 日下午 12:57
1900 年 1 月 1 日上午 12:00

我正在尝试的格式如下:

DateTime.ParseExact(date, "MMM dd yyyy hh:mmtt", CultureInfo.InvariantCulture)

但这会引发 FormatException。有什么建议吗?

【问题讨论】:

  • 您能否澄清Custom Date and Time Format strings 的哪一部分不清楚日期格式(例如"d""dd" 的区别)?
  • 请注意,帖子的标题与您遇到的问题完全无关 - 您可能需要重新阅读 minimal reproducible example 为帖子创建示例代码的指南。
  • 检查日期字符串中的尾随空格
  • 尝试修剪空白,仍然抛出错误。这可能与 AM/PM 部分在几分钟之后有关吗?
  • “尝试修剪空白,仍然抛出错误。” -- 哪个字符串抛出错误?您有两个字符串,但您的格式仅适用于其中一个。在字符串Jan 1 1900 12:00AM 上使用"MMM dd yyyy hh:mmtt" 格式时,完全可以预期您将获得FormatException。改用这种格式:"MMM d yyyy hh:mmtt",然后修剪空格,然后告诉我们您是否仍然在 both 字符串上遇到格式异常。

标签: c# .net datetime


【解决方案1】:

您可能需要查看 MSDN 上的 Custom Date and Time Format Strings 页面。我认为您的问题是您当前的格式在您的一天和一小时都需要前导零。例如,这看起来像

1900 年 1 月 1 日下午 1:00

您可能想要使用该格式

DateTime.ParseExact(date, "MMM d yyyy h:mmtt", CultureInfo.InvariantCulture)

它不需要前导零用于日期和小时位置。这将匹配诸如

之类的值

1900 年 1 月 1 日下午 1:00

我希望这会有所帮助。

【讨论】:

  • 在 1900 年 1 月 1 日凌晨 12:00 尝试这种格式仍然不起作用。你的回答对我来说似乎是正确的。
  • @pcounan 它对我来说很好用。在您的帖子中,您提供了两个示例字符串,第一个字符串有一个尾随空格。除了您的格式问题(都可以通过使用此答案中给出的格式来解决),您还需要删除尾随空格,因为它们也会导致FormatException。可能使用Trim() 去掉所有前导和尾随空格。
  • 很抱歉给您带来了困惑。我提供的格式不适用于我提供的值。该值是您在问题中提供的格式将匹配哪些值的示例。我的格式应该使用诸如 Jan 1 1900 1:00AM 之类的值,而不是与原始格式匹配的值 Jan 01 1900 01:00AM。我提供的格式不需要前导零。你的可以。这是唯一的区别。
  • 发现了错误,将字符串放入数据库的开发人员用空格替换了会在一天或一小时出现的前导零,因此 Jan 和 1 之间有两个空格。目前正在解决一个解决方案。
  • @pcounan 如果你没有注意到我的回答,它确实解决了问题。
【解决方案2】:

DateTime.ParseExact 有一个重载,它采用一组格式来尝试并让您指定允许使用空格:

string[] ss = new string[] { "Jun 29 2016 6:59PM", "Jan 1  1900 12:00AM " };
string[] formats = new string[] { "MMM dd yyyy h:mmtt", "MMM d yyyy hh:mmtt", "MMM dd yyyy hh:mmtt", "MMM d yyyy h:mmtt" };

foreach (string s in ss)
{
    Console.WriteLine(DateTime.ParseExact(s, formats, null, DateTimeStyles.AllowWhiteSpaces).ToString("yyyy-MM-dd HH:mm:ss"));
}

但如果您的日期字符串有点松散,您可以改用TryParse

DateTime d;
foreach (string s in ss)
{
    if (DateTime.TryParse(s, out d))
    {
        Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"));
    }
}

您可能应该指定一个 System.IFormatProvider。

【讨论】:

    【解决方案3】:

    第一个字符串将被成功解析。

    第二个字符串需要使用d specifier 而不是dd specifier,因为单日数字有前导零。

    DateTime.ParseExact("Jun 29 2016 12:57PM", 
                        "MMM dd yyyy hh:mmtt", 
                         CultureInfo.InvariantCulture).Dump();
    

    DateTime.ParseExact("Jan 1 1900 12:00AM ", 
                        "MMM d yyyy hh:mmtt", 
                         CultureInfo.InvariantCulture).Dump();
    

    顺便说一句,你可以使用MMM d yyyy hh:mmtt格式。

    【讨论】:

    • 您可以在两个字符串上使用d
    • @Quantic 是的,但这可能没有必要,因为 hhhparses 12 都成功了。
    • @Downvoter 至少愿意发表评论,这样我才能看到我可能哪里出错了?
    【解决方案4】:

    这是日期格式的备忘单,由http://www.mikesdotnetting.com/article/23/date-formatting-in-c提供

      Example Usage
    
     <%= String.Format("{specifier}", DateTime.Now) %>
     @DateTime.Now.ToString("F")
     @DateTime.Now.ToString("hh:mm:ss.fff")
    
    
    Specifier
    
    Description
    
    Output
    
    d Short Date 08/04/2007 
    D Long Date 08 April 2007 
    t Short Time 21:08 
    T Long Time 21:08:59 
    f Full date and time 08 April 2007 21:08 
    F Full date and time (long) 08 April 2007 21:08:59 
    g Default date and time 08/04/2007 21:08 
    G Default date and time (long) 08/04/2007 21:08:59 
    M Day / Month 08 April 
    r RFC1123 date Sun, 08 Apr 2007 21:08:59 GMT 
    s Sortable date/time 2007-04-08T21:08:59 
    u Universal time, local timezone 2007-04-08 21:08:59Z 
    Y Month / Year April 2007 
    dd Day 08 
    ddd Short Day Name Sun 
    dddd Full Day Name Sunday 
    hh 2 digit hour 09 
    HH 2 digit hour (24 hour) 21 
    mm 2 digit minute 08 
    MM Month 04 
    MMM Short Month name Apr 
    MMMM Month name April 
    ss seconds 59 
    fff milliseconds 120 
    FFF milliseconds without trailing zero 12 
    tt AM/PM PM 
    yy 2 digit year 07 
    yyyy 4 digit year 2007 
    : Hours, minutes, seconds separator, e.g. {0:hh:mm:ss} 09:08:59 
    / Year, month , day separator, e.g. {0:dd/MM/yyyy} 08/04/2007 
    . milliseconds separator 
    

    【讨论】:

    • 这是什么? o.O 我们真的需要这个吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2011-12-29
    • 1970-01-01
    相关资源
    最近更新 更多