How to get date field from MM / dd / yyyy to yyyy / MM / dd in vb.net
I need to get a date field from MM / dd / yyyy to yyyy / MM / dd in vb.net, but after that it should be a date field so that I can match it to a date in the database.
At the moment, all I manage to do is change it to a string in this format.
I tried this type of code which also didn't work.
DateTime.Parse(yourDataAsAString).ToString("yyyy-MM-dd")
fromDeString = String.Format("{0:yyyy/MM/dd}", aDate)
fromDate = Format("{0:yyyy/MM/dd}", aDate)
Any help would be greatly appreciated, thanks
a source to share
You donโt understand that the date object does not store numbers in any particular format. The only way to get the digits formatted in the order you want is to convert it to a string. Why would you want to compare them in a specific format? Date is the date, no matter how it is formatted. 12/15/78 == 1978/12/15.
If you cannot compare dates from DB to date object in VB, it is likely that the date you are comparing against the database is returned to you in string format, in which case you should hide it to the date object for comparison.
Dim sDate As String = "2009/12/15" 'Get the date from the database and store it as a string
Dim dDate As New Date(2009, 12, 15) 'Your date object, set to whatever date you want to compare against
Select Case Date.Compare(dDate, Date.Parse(sDate))
Case 0
'The dates are equal
Case Is > 0
'The date in the database is less
Case Is < 0
'The date in the database is greater
End Select
Here's an example of a module that demonstrates the desired functionality.
Imports System.Globalization
Module Module1
Sub Main()
Dim culture As New CultureInfo("en-us", True)
Dim mmDDyy As String = "10/23/2009"
Dim realDate As Date = Date.ParseExact(mmDDyy, "mm/dd/yyyy", culture)
Dim yyMMdd As String = realDate.ToString("yyyy/MM/dd")
End Sub
End Module
Hope it helps.
Regards Noel
a source to share