I want to compare two date formats and return \"false\" when two formats are not equal.
For example, I get two da
You are asking a question (or two) which does not need to be answered.
Dates do not have a format Formats are how dates are displayed to humans. A date is simply a very large number like 636094492018399433L
. It does not have a format.
I want a function thats returns false when the second format to compare not equal the SQL format (YYYY-MM-DD)
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.
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
as DateTime
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
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.
Try like
Dim matcher As Boolean = myString Like "??/??/????"
28/11/1997; matcher = True
BUT 11/28/1997; matcher = True
(is also true)
It works for some patterns but not for everything maybe try splitting the date into more than one variable :)
The question is confusing because it asks two separate questions. This is an answer to the second question which asks for a function that will test whether a date string is in the format "yyyy-MM-dd". It uses the DateTime.ParseExact
method to test whether the string is in the required format.
Function IsCorrectDateFormat(testDate As String) As Boolean
Dim myDate As DateTime
Return DateTime.TryParseExact(testDate, "yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, myDate)
End Function