c# -如何将字符串转换为时间戳以插入mysql

本文关键字:时间戳 插入 mysql 转换 字符串 | 更新日期: 2023-09-27 18:13:29

我使用c#和Mysql数据库。如何将字符串转换为时间戳插入mysql?例如,我有string:28.9.2015 05:50:00

c# -如何将字符串转换为时间戳以插入mysql

DateTime。ParseExact是你需要的:

DateTime date = DateTime.ParseExact("28.9.2015 05:50:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

您可以指定所需的格式并将其转换为如下的日期时间

DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

我找不到这么难的地方…简单地使用DateTime.Parse方法

DateTime date = DateTime.Parse("28.9.2015 05:50:00");

不知道它是否会工作,但试试这个

DateTime date = DateTime.ParseExact("28.9.2015 05:50:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

现在你可以把它作为一个date类型插入到你的数据库中。

古德勒克。

使用DateTime。ParseExact:

using using System.Globalization;
string date = "31/12/2018";
dateParsed = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

查询数据库:

using (MyAppContext c = new MyAppContext())
{
    foreach (DbValues dbValues in c.DbValues.Where(a=> a.Timestamp < dateParsed))
    {
        ...
    }
}