Solved

Need to convert integer to month name (English and French) in VB.Net

Posted on 2012-12-30
3
2,230 Views
Last Modified: 2012-12-31
Hi,

I need to convert a month integer into a month name, for both French and English, in VB.Net.

I would prefer to use some type of included Globalization parameters (such as CultureInfo.CreateSpecificCulture("fr-FR")) to do the conversion, rather than doing a "select case" and just listing all the month names in every language - since I may add a 3rd language soon to my site and want to keep my code clean.

For instance to format dates, I am currently using:

FrenchDate = mydatetime.ToString("dddd, 'le ' d MMMM yyyy, ' à ' h'h'mm", CultureInfo.CreateSpecificCulture("fr-FR"))

... but I cannot seem to find an equivalent I can use for an integer.

My integers are not preceeded by zeros (ie: they are 1,2,3,4,5,6,7,8,9,10,11,12) and I would like the full month name (ie: January or Janvier)

Thanks, Peter
0
Comment
Question by:pbissegger
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 38731709
Just create a new DateTime instance that has the appropriate month set.

e.g.

mydatetime = New DateTime(DateTime.Now.Year, 1, DateTime.Now.Day)
mydatetime = New DateTime(DateTime.Now.Year, 3, DateTime.Now.Day)
mydatetime = New DateTime(DateTime.Now.Year, 5, DateTime.Now.Day)

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 38731809
Full example...using kaufmed's idea:
        Dim French As New Globalization.CultureInfo("fr-FR")
        Dim English As New Globalization.CultureInfo("en-US")

        For m As Integer = 1 To 12
            Dim dt As New DateTime(DateTime.Today.Year, m, 1)
            Debug.Print("English: " & dt.ToString("MMMM", English))
            Debug.Print("French: " & dt.ToString("MMMM", French))
        Next

Open in new window

0
 

Author Comment

by:pbissegger
ID: 38732456
It works ! Here is my final solution:

Function Monthonly(monthnumber as Integer) as String
        
        Dim French As New System.Globalization.CultureInfo("fr-FR")
        Dim English As New System.Globalization.CultureInfo("en-US")
        
        Dim dt As New DateTime(DateTime.Today.Year, monthnumber, 1)

        If Session("lang") = "english" Then
            Monthonly = dt.ToString("MMMM", English)
        else
            Monthonly = dt.ToString("MMMM", French)
        End If

End Function

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now