I just want to ask how VB.NET perform is conversion of date from string. I am currently designing a database view and convert the date into MMddyyyy
format, but I am worried that CDATE it will read it as ddMMyyyy
.
I want it to make it shorter and converting the date using CDATE instead of using the traditional M/d/yyyy h:mm:ss tt
.
Example:
Dim myDate As Date = CDate(DataTable.Rows(0).Item("DateValue").ToString())
CDate depends on the control panel regional settings and is not recommended - you should use Date.ParseExact
instead
Const MyDateFormat As String = "MMddyyyy"
Dim dte As Date = #2/1/2003#
'convert the date to a string
Dim strDate As String = dte.ToString(MyDateFormat)
'convert the string back to a date
Dim dte2 As Date = Date.ParseExact(strDate, MyDateFormat, System.Globalization.CultureInfo.InvariantCulture)
If dte = dte2 Then
MsgBox("They're the same :-) " & strDate)
Else
MsgBox("They're different :-(")
End If
For your code, it would look like:
Dim myDate As Date = Date.ParseExact(DataTable.Rows(0).Item("DateValue").ToString(), "MMddyyyy", System.Globalization.CultureInfo.InvariantCulture)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With