【问题标题】:How to convert a mysql datetime string to c# datetime string?如何将 mysql 日期时间字符串转换为 c# 日期时间字符串?
【发布时间】:2016-03-13 09:33:48
【问题描述】:

我从 mysql db 得到“10/1/15 12:00:00 AM" 时间字符串。如何通过ParseExact 方法将其转换为System.DateTime

【问题讨论】:

  • 你确定你得到的是String吗?而不是已经映射的DateTime

标签: c# mysql datetime


【解决方案1】:

您可以使用DateTime.ParseExact 方法和自定义日期和时间格式。

我假设你的10 是一天;

var dt = DateTime.ParseExact("10/1/15 12:00:00 AM", "dd/M/yy hh:mm:ss tt", 
                             CultureInfo.InvariantCulture);

顺便说一句,在数据库中将DateTime 保存为string 当然是个坏主意。如果可以,将列类型更改为 DATETIME 数据类型。

阅读:Bad habits to kick : choosing the wrong data type

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多