Click here to Skip to main content
15,914,608 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
in My Application Display Date Format Is dd/MM/yyyy and my client computer Date time format is MM/dd/yyyy so My Application is Throw Error Data time Convert please help me This Proble
Posted
Updated 9-May-14 0:32am
v3
Comments
CHill60 9-May-14 6:27am    
Share the code that is throwing the error
King Fisher 9-May-14 6:29am    
what format do you need?
'dd/MM/yyy' or 'MM/mm/yyyy'

use ....
DateTime.ParseExact (sampledatetime,"dd/MM/yyyy hh:mm:ss tt" ,CultureInfo.InvariantCulture );
 
Share this answer
 
try with DateTime.ParseExact with format
C#
DateTime convertedDate = DateTime.ParseExact(inputDateString, dd/MM/yyyy, CultureInfo.InvariantCulture);
 
Share this answer
 
Do not rely on a specific DateTime format: the "standard" format for the local computer as set in Regional and Language settings of the computer. Then DateTime.Parse/TryParse will work when the Date was entered with the "typical" format.
 
Share this answer
 
To support the other two solutions I thought a worked example might be in order

This function will return a value of True if the conversion was successful and outDate will be the Date. outDate will (usually) be set to 01/01/01 if the function returns False - do not rely on this
VB
Private Function DateExample(ByVal inDate As String, ByVal culture As System.Globalization.CultureInfo, ByRef outDate As Date) As Boolean

    Dim itWorked As Boolean = Date.TryParseExact(inDate, "d", culture, System.Globalization.DateTimeStyles.None, outDate)
    Return itWorked

End Function

The function uses the TryParseExact [^]method to try to convert a date represented by a string using the "culture" settings defined. Note the lower-case d - that means I'm using a "short date" pattern... here is the list of standard patterns
[^]
Let's call that function ...
VB
Private Function MessageToDisplay(ByVal inDate As String, ByVal inCulture As String) As String
    Dim culture As System.Globalization.CultureInfo = New System.Globalization.CultureInfo(inCulture)
    Dim aDate As Date
    Dim itWorked As Boolean = DateExample(inDate, culture, aDate)

    Dim retMsg As String
    If itWorked Then
        retMsg = "Date " + inDate + " with Culture " + inCulture + " = " + aDate.ToString(culture)
    Else
        retMsg = "Invalid Date " + inDate + " or culture " + inCulture
    End If

    Return retMsg

End Function

This function takes the required date represented as a string, and which culture we want the results in (i.e. format). Here is the list of language codes avaiable[^]. The function will return a message which will be different depending on whether or not the date could be provided in that culture.

Finally, here are some examples ...
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim USDate As String = "05/30/2014"
    Dim USCulture As String = "en-US"

    Dim UKDate As String = "30/05/2014"
    Dim UKCulture As String = "en-GB"

    'Try US date first, with US format
    MessageBox.Show(MessageToDisplay(USDate, USCulture))

    'Do the same with UK date, but tell the function we wanted it in US format
    'We expect an error here!
    MessageBox.Show(MessageToDisplay(UKDate, USCulture))

    'This one is interesting ... change the UK Date to 5th March 2014
    UKDate = "05/03/2014"
    MessageBox.Show(MessageToDisplay(UKDate, USCulture)) 'Displays 3rd May 2014 !

End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900