【问题标题】:Converting string with month that is not integer to datetime [duplicate]将带有非整数月份的字符串转换为日期时间[重复]
【发布时间】:2013-07-04 09:20:09
【问题描述】:

我有格式为

的字符串

2013 年 3 月 25 日下午 6:30

我想将它们转换为格式的日期时间对象

2013-03-25 18:30:00

我该怎么做呢?

【问题讨论】:

标签: c#


【解决方案1】:

试试这个

DateTime dt
CultureInfo provider = CultureInfo.InvariantCulture;
System.Globalization.DateTimeStyles style = DateTimeStyles.None;
DateTime.TryParseExact("Mar 25 2013 6:30PM", "MMM d yyyy h:mtt", provider, style, out dt);

【讨论】:

  • -1,很确定你的格式是错误的。至少tt之前的空格@
  • @musefan 我已经纠正了。现在工作.tested
  • 是的,我也测试了您的更新。效果很好
【解决方案2】:

你可以使用:

 var date = DateTime.Parse("Mar 25 2013 6:30PM");

 Console.WriteLine(date.ToString()); /*This will output the date in the required format */

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多