asp.net | Function to convert date from UK to USasp.net |

I created a function and I call it whenever there is a date value, as the default date format for SQL database is yyyy-mm-dd

Simply import the class

Import date_conv

feed it with the date you would like to convert

dim date_value as Datetime = convdate(txt_my_date.text)

New Function

    Shared Function conv_date(ByVal d As String) As String

        Dim date_in As DateTime = DateTime.Parse(d.ToString)

        Dim x As String

        x = date_in.Year & "-" & date_in.Month & "-" & date_in.Day

        Return x

    End Function

The Function code (old code / it has few bugs when there are multi culture in the project)

Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class convertDate

    Shared Function conv_date(ByVal d As String) As DateTime

        Dim MyCultureInfo As CultureInfo = New CultureInfo("en-GB")
        Dim MyString As String = d.ToString
        Dim MyDateTime As DateTime = DateTime.Parse(MyString, MyCultureInfo, DateTimeStyles.NoCurrentDateDefault)

        Return MyDateTime

    End Function

End Class

Remember to add “Check if date is correct”


Leave a Reply

Your email address will not be published. Required fields are marked *