如何比较两个日期FORMATS保存到DB [英] How to compare two dates FORMATS for saving to DB
问题描述
如果两种格式不等于,我想比较两种日期格式,并返回false。
I want to compare two date formats and return "false" when two formats are not equal.
例如,我收到两个日期,24/10/2012(DD / MM / YYYY)和2016/11/05(YYYY / MM / DD)..在这种情况下,一些函数应该返回false,因为日期格式不等于。
For example, I get two dates, 24/10/2012 (DD/MM/YYYY) and 2016/11/05 (YYYY/MM/DD)... in this case some function should return false because date formats are not equal.
我想要一个函数,当第二个格式为比较不等于SQL格式(YYYY-MM-DD)。
I want a function thats returns false when the second format to compare not equal the SQL format (YYYY-MM-DD).
推荐答案
你在问一个(或两个)不需要回答。
You are asking a question (or two) which does not need to be answered.
日期没有格式 格式是日期显示给人类。日期只是一个非常大的数字,如 636094492018399433L
。它没有格式。
我想要一个函数,当第二种格式的比较不等于SQL格式(YYYY-MM -DD)
您真的不必担心使用NET DB提供程序的数据库格式(例如OleDB,SQLite,SQL Server,MySQL) 。他们都知道如何正确地将日期数据存储到日期列 - 它的工作。如果您的列是字符串,不要这样做。如果您希望日期按日期行事,请将其存储为日期。
You really need not worry about the db format using the NET DB providers (e.g. OleDB, SQLite, SQL Server, MySQL). They all know how to properly store date data to a date column - its their job. If your columns are string, don't do that. If you want dates to act like dates, store them as dates.
数据库文档很难解释通过键盘从Shell界面输入数据的情况下的日期格式,或者可能从文本/ csv文件导入数据。当使用NET DB提供商时,数据格式是一个实现细节。
Database docs bother to explain date formats for cases where you are entering data via a Shell interface from the keyboard, or perhaps importing data from a text/csv file. When using the NET DB Providers, the data format is an implementation detail.
Using dbCon As New MySQLConnection(mySQLConnStr)
Using cmd As New MySqlCommand(SQL, dbCon)
dbCon.Open()
cmd.Parameters.Add("@p1", MySqlDbType.DateTime).Value = fromDate
cmd.Parameters.Add("@p2", MySqlDbType.DateTime).Value = toDate
cmd.ExecuteQuery
End Using
End Using
- 将
DbType
指定为DateTime
- 传递日期数据。
- specify the
DbType
asDateTime
- pass it Date data.
要仅存储日期,大多数DB都有一个单独的 DbType.Date
,但通常你只需要通过 .Date
部分:
To Store just the date, most DBs have a separate DbType.Date
, but often you need to only pass the .Date
portion:
cmd.Parameters.Add("@p2", MySqlDbType.Date).Value = toDate.Date
NET DB提供程序都知道事情,就像如何使用NET Date并将其保存到他们构建的数据库中,并以可以解析/读取的格式执行。
The NET DB Providers all Know Things, like how to take a NET Date and save it to the database they were built for, and do so in a format it can parse/read back from.
这篇关于如何比较两个日期FORMATS保存到DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!