Monday, October 26, 2009

DateTime String Format In Asp.net

Here are some of the string formats that you can try out:
Specifier
type
output
(June 8, 1970 12:30:59)
dd
Day
08
ddd
Short Day Name
Mon
dddd
Full Day Name
Monday
hh
2 digit hour
12
HH
2 digit hour (24 hour)
12
mm
2 digit minute
30
MM
Month
06
MMM
Short Month name
Jun
MMMM
Month name
June
ss
seconds
59
tt
AM/PM
PM
yy
2 digit year
70
yyyy
4 digit year
1970
:
seperator, e.g. {0:hh:mm:ss}
12:30:59
/
seperator, e.g. {0:dd/MM/yyyy}
08/06/1970

Try this way

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="CheckDateFormat" DataField="Date" DataFormatString="{0:M-dd-yyyy}" />
</Columns>
</asp:GridView>
---
DateTime dt = DateTime.Now;
String strDate="";
strDate = dt.ToString("MM/dd/yyyy");   // 07/21/2007 

strDate = dt.ToString("dddd, dd MMMM yyyy");   //Saturday, 21 July 2007

strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm"); // Saturday, 21 July 2007 14:58

strDate = dt.ToString("dddd, dd MMMM yyyy hh:mm tt"); // Saturday, 21 July 2007 03:00 PM

strDate = dt.ToString("dddd, dd MMMM yyyy H:mm"); // Saturday, 21 July 2007 5:01 

strDate = dt.ToString("dddd, dd MMMM yyyy h:mm tt"); // Saturday, 21 July 2007 3:03 PM

strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); // Saturday, 21 July 2007 15:04:10

strDate = dt.ToString("MM/dd/yyyy HH:mm"); // 07/21/2007 15:05

strDate = dt.ToString("MM/dd/yyyy hh:mm tt"); // 07/21/2007 03:06 PM

strDate = dt.ToString("MM/dd/yyyy H:mm"); // 07/21/2007 15:07

strDate = dt.ToString("MM/dd/yyyy h:mm tt"); // 07/21/2007 3:07 PM

strDate = dt.ToString("MM/dd/yyyy HH:mm:ss"); // 07/21/2007 15:09:29

strDate = dt.ToString("MMMM dd"); // July 21

strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"); // 2007-07-21T15:11:19.1250000+05:30    

strDate = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"); // Sat, 21 Jul 2007 15:12:16 GMT

strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); // 2007-07-21T15:12:57

strDate = dt.ToString("HH:mm"); // 15:14

strDate = dt.ToString("hh:mm tt"); // 03:14 PM

strDate = dt.ToString("H:mm"); // 5:15

strDate = dt.ToString("h:mm tt"); // 3:16 PM

strDate = dt.ToString("HH:mm:ss"); // 15:16:29

strDate = dt.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'"); // 2007-07-21 15:17:20Z

strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); // Saturday, 21 July 2007 15:17:58

strDate = dt.ToString("yyyy MMMM"); // 2007 July




Hence we can format and getting the datetime value.

No comments:

Post a Comment

Ideal SQL Query For Handling Error & Transcation in MS SQL

BEGIN TRY BEGIN TRAN --put queries here COMMIT; END TRY BEGIN CATCH IF @@TRANCOUNT>0 BEGIN SELECT @@ERROR,ERRO...