Hi all,

I need a more elegant way to do the convertion between different cultures

First I parse the field into the default culture (en-US) like this,

Dim Format As New System.Globalization.CultureInfo("en-US", True)
Dim sFecha As Date = System.DateTime.Parse(me.TextBox1.Text.Trim, Format, Globalization.DateTimeStyles.None)

What I need is a way to show in another textbox in another culture, obviously I have the option to do a: TextBox2.Text = Format(me.TextBox1.Text.Trim, "dd/MM/yyyy") but I need to be configurable to any culture selected by the user.

I hope you can help me...

Thanks

Dani AI

Generated

As described, the clean approach is: parse the input once into a DateTime, then format that DateTime for display using the target CultureInfo. Avoid changing the thread/global culture on the server for a single display — use the ToString overloads that accept an IFormatProvider so formatting is explicit and thread-safe. Use TryParse (or ParseExact if the input format is fixed) to handle invalid or ambiguous input safely.

Example (VB.NET):

' parse using known input culture (e.g. en-US)
Dim inputCi As New Globalization.CultureInfo("en-US")
Dim dt As DateTime
If DateTime.TryParse(txtInput.Text.Trim(), inputCi, Globalization.DateTimeStyles.None, dt) Then
    ' user-selected culture name like "fr-FR" or "es-ES"
    Dim targetCi As New Globalization.CultureInfo(selectedCultureName)
    txtOutput.Text = dt.ToString("d", targetCi)  ' "d" = short date pattern for that culture
Else
    txtOutput.Text = "Invalid date"
End If

Practical notes: populate the culture list with CultureInfo.GetCultures, catch CultureNotFoundException if the user selection is invalid, and prefer storing DateTime values (not strings) in the database. For ambiguous user input (01/02/2008) require a culture or use ParseExact with a known pattern. For page-wide rendering you can set culture in web.config or the page directive, but for one-off conversions prefer explicit ToString with the chosen CultureInfo. See the Microsoft docs for CultureInfo and DateTime.ToString for details: CultureInfo class and DateTime.ToString.

Anyone?, Plzzzz, this is driving me crazy!!

!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.