C# - 如何将字符串转换为时间戳以插入 mysql
C# - How to convert string to timeStamp for insert into mysql
我正在使用 C# 和 Mysql 数据库。
如何将字符串转换为时间戳以插入 mysql?例如,我有字符串:
28.9.2015 05:50:00
我在这里找不到这么难的东西...只需使用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(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
DateTime.ParseExact是你需要的:
DateTime date = DateTime.ParseExact("28.9.2015 05:50:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
使用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))
{
...
}
}
我正在使用 C# 和 Mysql 数据库。
如何将字符串转换为时间戳以插入 mysql?例如,我有字符串:
28.9.2015 05:50:00
我在这里找不到这么难的东西...只需使用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(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
DateTime.ParseExact是你需要的:
DateTime date = DateTime.ParseExact("28.9.2015 05:50:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
使用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))
{
...
}
}