Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How CDATE perform its conversion from string to date on VB.NET

Tags:

date

vb.net

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())
like image 680
pvzkch Avatar asked May 25 '15 07:05

pvzkch


1 Answers

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)
like image 64
SSS Avatar answered Oct 19 '22 16:10

SSS