Skip to content
March 10, 2008 / kiranpatils

Formatting Date and Time in GridView

Today, I am playing with Gridview and have to show data in it from SQL SERVER 2005. it has a field called as “JoiningDate” bounded with column “JoiningDate”. it was working fine it was showing date in the format like this: 2007-11-27 10:00:00.000 but i want to show it like this “11 Dec 2007”. I found it from MSDN. so here it is!!

Old Code[O/P=2007-11-27 10:00:00.000]:

<asp:BoundField DataField=”JoiningDate” HeaderText=”JoiningDate” / >

New one [O/P = 11 Dec 2007]

<asp:BoundField DataField=”JoiningDate” HeaderText=”JoiningDate” DataFormatString=”{0:dd MMM yyyy}” HtmlEncode=”False”/ >

Explanation

Two main tags helped me too achieve this:

1. DataFormatString: you can specify format to display string.

dd = Date

MMM = Month

yyyy = year

2. HtmlEncode: the field are HTML Encoded before they shown to bound field.Default true

NOTE: if you don’t do HTMLEncode=”False” .it won’t work. Because default it true.

References:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp

http://msdn2.microsoft.com/en-us/library/system.datetime.tostring(VS.71).aspx

Hope it will help you too as it helped me..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: