Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

i would like to convert date to year and month format, please help
i used DateTime.Now.ToString("y") and DateTime.Now.ToString("m") but its not working.
please advice
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jun-12 17:13pm    
What is "not working"? This is not descriptive. What was wrong?
--SA

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jun-12 17:15pm    
That's all OP would need to use, my 5.
--SA
Use the simple code shown below:

String datemonth = DateTime.Now.Year.ToString() + ":"+ DateTime.Now.Month.ToString();

This you can format and have greater flexibility
 
Share this answer
 
Try:
C#
String s = DateTime.Now.ToString("MMM yyyy");
There is a list of the formats here: Formatting a DateTime for display - format string description[^]

Do note that these conversions are culture specific, so if you run this on a server it will not necessarily produce a local date on a client.
 
Share this answer
 
Comments
sandeep nagabhairava 25-Jun-12 13:15pm    
my +5
Sergey Alexandrovich Kryukov 25-Jun-12 17:14pm    
Sure, a 5.
--SA
 
Share this answer
 
Comments
Pankaj Nikam 25-Jun-12 5:51am    
+5 :)
Vani Kulkarni 25-Jun-12 23:28pm    
Thanks! :)
Sandeep Mewara 25-Jun-12 12:31pm    
My 5!
Vani Kulkarni 25-Jun-12 23:28pm    
Thank you :)

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