dd-MM-yy hh:mmtt格式的日期如何转换成DateTime?

xsan_3 2014-04-29 06:58:32
dd-MM-yy hh:mmtt格式的日期如何转换成DateTime?
例如:11-12-14 08:22AM如何转换成DateTime
...全文
837 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
但是,你要注意你的数据,是不是运行在不同地区的机器上,不同格式转换的时候考虑使用CultureInfo.CurrentCulture
wg5945 2014-04-29
  • 打赏
  • 举报
回复
dd-MM-yy hh:mmtt还真不行~~ 只能是MM-dd-yy hh:mmtt
  • 打赏
  • 举报
回复
            var t = "11-12-14 08:22AM";
            var dt = DateTime.ParseExact(t, "dd-MM-yy hh:mmtt", new CultureInfo("en-US"));
这样就好了
wg5945 2014-04-29
  • 打赏
  • 举报
回复
看错了,请无视~~
wg5945 2014-04-29
  • 打赏
  • 举报
回复
public static DateTime? ConvertStrToDate(string value, string format)
{
    if (string.IsNullOrEmpty(value))
    {
        return null;
    }
    return DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
}
  • 打赏
  • 举报
回复
有个AM比较难搞,这个应该是和当前环境(区域、语言)有关的。 例如在我的电脑上DateTime.ToString("dd-MM-yy hh:mmtt");出来的是29-04-14 09:22上午 如果没有这个标记就简单了:
            var t = "11-12-14 08:22";
            var dt = DateTime.ParseExact(t, "dd-MM-yy hh:mm", null);
  • 打赏
  • 举报
回复
Convert.ToDateTime("11-12-14 08:22AM")

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧