show date in two languages

W

Willie jan

Hi,

i want to show the current date in two languages.
when i use this code it works, but i have to set the application to the
culture and because this will become a function inside a usercontrol, i do
not want to change the culture for the application just to get a different
language text...

Anyone an idea?

Dim c As CultureInfo
c = New CultureInfo("en-US")
Application.CurrentCulture = c
Dim sEnglish As String = Format(Now, c.DateTimeFormat.LongDatePattern())

c = New CultureInfo("fr-CH")
Application.CurrentCulture = c
Dim sFrance As String = Format(Now, c.DateTimeFormat.LongDatePattern())
 
L

Larry Lard

Willie said:
Hi,

i want to show the current date in two languages.
when i use this code it works, but i have to set the application to the
culture and because this will become a function inside a usercontrol, i do
not want to change the culture for the application just to get a different
language text...

Anyone an idea?

Dim c As CultureInfo
c = New CultureInfo("en-US")
Application.CurrentCulture = c
Dim sEnglish As String = Format(Now, c.DateTimeFormat.LongDatePattern())

c = New CultureInfo("fr-CH")
Application.CurrentCulture = c
Dim sFrance As String = Format(Now, c.DateTimeFormat.LongDatePattern())

You have the right idea, it's just a matter of searching around in the
Framework for the right method. As it happens, the DateTime type (Date
in VB) overrides its ToString method in a number of ways, one of which
provides exactly what you want:

Imports System.Globalization

Dim dt As Date = Now
Dim US As CultureInfo = New CultureInfo("en-US")
Dim FR As CultureInfo = New CultureInfo("fr-FR")

MsgBox(dt.ToString("D", US.DateTimeFormat))
MsgBox(dt.ToString("D", FR.DateTimeFormat))

Here "D" specifies Long Date (the "DateTimeFormatInfo Class" help topic
has the list).
 
W

Willie jan

thanks, this helps me a lot.

Willie Jan.

Larry Lard said:
You have the right idea, it's just a matter of searching around in the
Framework for the right method. As it happens, the DateTime type (Date
in VB) overrides its ToString method in a number of ways, one of which
provides exactly what you want:

Imports System.Globalization

Dim dt As Date = Now
Dim US As CultureInfo = New CultureInfo("en-US")
Dim FR As CultureInfo = New CultureInfo("fr-FR")

MsgBox(dt.ToString("D", US.DateTimeFormat))
MsgBox(dt.ToString("D", FR.DateTimeFormat))

Here "D" specifies Long Date (the "DateTimeFormatInfo Class" help topic
has the list).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top