Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get month number from given date,Suppose given date is 01/21/2014
output:january .how to do that??
Posted

Try this

C#
string name = DateTime.ParseExact("01/21/2014", "MM/dd/yyyy", null).ToString("MMMM"); //January 
 
Share this answer
 
Comments
Rahul VB 30-Jan-14 12:44pm    
Great stuff. Hey please explain me :ParseExact.
Karthik_Mahalingam 30-Jan-14 12:47pm    
see if you use Convert.ToDatetime(some); if the string is not valid to the cultureinfo then it will throw string incorrect format error.
so to avoid that ( if we know the format of the date string we can parse exactly the date object )
from the example it is clear that the date is in MM/dd/yyyy format so i used this code..
hope you understood.
Rahul VB 30-Jan-14 12:53pm    
hey captain! thanks a ton.
Karthik_Mahalingam 30-Jan-14 12:54pm    
Welcome Rahul :)
Rahul VB 30-Jan-14 13:07pm    
Still awake? no office tomorrow? :)
Hello, to find out the month: look at the following code:


C#
String[] s = DateTime.Now.ToString().Split('-',' ');


       for (int i = 0; i < s.Length; i++)
       {

           switch (i)
           {
               case 0:
                   Console.WriteLine("Date is :{0}",s[i]);
                   break;
               case 1: 
                   if(s[i] == "01")////this check is just for january, you can do it for all other months
                   Console.WriteLine("Month January");
                   break;
           }

      }



I have given you a logic. However this is a very bad and redundant coding. Its just a way so that you can use this to develop your own logic.

Just confirm one thing: print the date time on the console. If you do so you can then decide which condition to write in the switch case.


You need to know how to format and operate on a particular string: i have used string's split method. There are multiple ways just, google them.

Why even do all the above things? simply say :


C#
Console.WriteLine(DateTime.Now.ToString("MM"));



Whats the reason to do all the above exercise? To just develop your logic? What if there was no help. These are all just wrappers. You need to write something of your own. Just grow from the situation.

Thanks,
Happy learning.
 
Share this answer
 
v5
If you input is a string the you have to parse[^] it in order to get a DateTime object, then, depending on your needs, you use the DateTime.Month[^] property or use the DateTime.ToString[^] method, passing "M" (or "MM") as argument.
 
Share this answer
 
Comments
Member 10523388 30-Jan-14 11:56am    
give me example to do so
Try Console.WriteLine(myDate.ToString("MMMM"))
More examples here[^].
 
Share this answer
 
Comments
Member 10523388 30-Jan-14 11:52am    
error: cannot convert from string to system.iformatprovider
Abhinav S 30-Jan-14 12:31pm    
Convert string to date time first.
Use DateTIme.TryParse.

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