Is there any function in C#, if we give the numeric value of a month it will give character as an output.
For example:
Input is say 1
Output is January
Input is say 12
Output is December
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jan 8, 2012, 6:36 AM EST
VulpesRank #55 · 29.2K reputationPosted Jan 8, 2012, 5:36 AM EST
Posted Jan 8, 2012, 5:56 AM EST
For example:
Console input is 1
Console output is January
Prabhu RajaRank #861 · 2.6K reputationPosted Jan 8, 2012, 6:00 AM EST
I think there is no Build in function in .Net to do this. You may try this function
1) Give " Microsoft.SqlServer.Smo " dll reference to your Project.
2) Try
using Microsoft.SqlServer.Management.Smo;
If this post helps you, then mark as "Correct Answer". Thank You
VulpesRank #55 · 29.2K reputationPosted Jan 8, 2012, 6:00 AM EST
Try it and see.
Posted Jan 8, 2012, 7:02 AM EST
string monthName = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m);
Please tell me the correct using directive for this.
Also thank you for your help.
using System;
class Test
{
static void Main()
{
Console.Write("Month No ");
int m = Convert.ToInt32(Console.ReadLine());
string monthName = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m);
Console.WriteLine(monthName);
Console.ReadKey();
}
}
VulpesRank #55 · 29.2K reputationPosted Jan 8, 2012, 7:12 AM EST
using System;
using System.Globalization;
class Test
{
static void Main()
{
Console.Write("Month No ");
int m = Convert.ToInt32(Console.ReadLine());
string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m);
Console.WriteLine(monthName);
Console.ReadKey();
}
}
Notice also that it's easy to get the month names in different languages with this approach. For example, if you change the above line to:
string monthName = CultureInfo.GetCultureInfo("fr-FR").DateTimeFormat.GetMonthName(monthNumber);
and enter 11 for monthNumber, then the output will be:
novembre
Posted Jan 8, 2012, 4:15 PM EST
using System;
using System.Globalization;
namespace _6e13_a_b_c_d_
{
class Program
{
static void Main(string[] args)
{
Friend[] friend = new Friend[3];
for (int x = 0; x < friend.Length; ++x)
{
friend[x] = new Friend();
Console.Write("Name #{0} ", x+1);
friend[x].setName(Console.ReadLine());
Console.Write("Phone No ");
friend[x].setPNo(Convert.ToInt32(Console.ReadLine()));
Console.Write("Birth Day (mm/dd/yyyy) ");
friend[x].setBday(Console.ReadLine());
Console.WriteLine();
}
Array.Sort(friend);
for (int x = 0; x < friend.Length; ++x)
Console.WriteLine("Name #{0} {1} Phone No {2} Birth Day {3}", x + 1, friend[x].getName(), friend[x].getPNo(), friend[x].getBday());
Console.WriteLine();
Console.Write("Enter friends name ");
string fn = Console.ReadLine();
for (int x = 0; x < friend.Length; ++x)
if (friend[x].getName()==fn)
Console.WriteLine("Phone No {0} Birth Day mm/dd/yyyy {1}", friend[x].getPNo(), friend[x].getBday());
Console.WriteLine();
Console.Write("Birthday month in a numeric format: ");
int m = Convert.ToInt32(Console.ReadLine());
string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m);
Console.WriteLine("Birthday is in {0}", monthName);
Console.ReadKey();
}
}
}
internal class Friend : IComparable
{
private string name;
private int pNo;
private string bDay;
public string getName()
{
return this.name;
}
public void setName(string name)
{
this.name = name;
}
public int getPNo()
{
return this.pNo;
}
public void setPNo(int pNo)
{
this.pNo = pNo;
}
public string getBday()
{
return this.bDay;
}
public void setBday(string bDay)
{
this.bDay = bDay;
}
public int CompareTo(Friend firstName)
{
return string.Compare(this.name, firstName.name);
}
}
VulpesRank #55 · 29.2K reputationPosted Jan 9, 2012, 6:45 AM EST
Posted Jan 9, 2012, 7:33 AM EST
Name #1 A
Phone No 123456
Birth Day (mm/dd/yyyy) 1/1/1111
Name #2 Q
Phone No 654321
Birth Day (mm/dd/yyyy) 2/2/2222
Name #3 Z
Phone No 123567
Birth Day (mm/dd/yyyy) 3/3/3333
Name #1 A Phone No 123456 Birth Day 1/1/1111
Name #2 Q Phone No 654321 Birth Day 2/2/2222
Name #3 Z Phone No 123567 Birth Day 3/3/3333
Enter friend's name A
At this point there is an error signal saying that Formal Exemptions was unhandled. It is indicating following step.
int m = int.Parse(bDay.Substring(0, 2));
Program is attached.
VulpesRank #55 · 29.2K reputationPosted Jan 9, 2012, 7:46 AM EST
You therefore need to enter a birth date in the format 01/01/1111 etc. or change it so that it can cope with single digit months or days.
Posted Jan 9, 2012, 9:41 AM EST
Thank you for your help.
int m = int.Parse(bDay.Substring(0, 2)); This code is new to me. Please give me relevant website addresses where I can learn about this code.
VulpesRank #55 · 29.2K reputationPosted Jan 9, 2012, 9:44 AM EST
http://msdn.microsoft.com/en-us/library/system.string.substring.aspx