Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my date is in format 2/1/2011 12:00:00 AM(database ) i want to diplay only month and year i.e in this case JAN 2011
Posted

from Code behind,
string dt=DateTime.Now.ToString("dd MMM yyyy");

from Database,-
getdate()
 
Share this answer
 
v2
SELECT MONTH(MyDateField) + ' ' + YEAR(MyDateField) AS ShowDate FROM ...

Will give you month and year. But it will give the month as a number, e.g. 2 for February. I don't know if there's any function in T-SQL that will give you the NAME of the month.

But you can easily do it in code:

Assuming that you have a DateTime variable, myDate, containing then date, you simply do:

string showMonth = myDate.ToString("MMM yyyy");

Good luck!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Apr-11 11:07am    
That, too. My 5.
--SA
DateTime dt = DateTime.Parse("2/1/2011 12:00:00 AM");
string date = dt.ToString("MMM yyyy");


Amazing the things that can be found when reading the documentation http://msdn.microsoft.com/en-us/library/k494fzbf.aspx[^]
 
Share this answer
 
v2
Comments
Johnny J. 13-Apr-11 9:43am    
This code will show "01 2011" - Not "JAN 2011"!
[no name] 13-Apr-11 9:48am    
Yep, mistyped. The CP editor doesn't have a syntax checker. Yet

Corrected
Sergey Alexandrovich Kryukov 13-Apr-11 11:07am    
Mark how come I do this syntax checker on line while editing in CP fields? Because I'm using SeaMonkey for a Browser, Firefox should provide it, too. Could it help you?
--SA
Sergey Alexandrovich Kryukov 13-Apr-11 11:07am    
By the way, my 5. :-)
--SA
You'll need to format the Date like this:

C#
var formatted = DateTime.Now.ToString("MMM yyyy");
 
Share this answer
 
Comments
#realJSOP 13-Apr-11 9:39am    
5 - Proposed as answer
Sergey Alexandrovich Kryukov 13-Apr-11 11:04am    
Sure, that's it, a 5.
--SA

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