C# ASP.NET String.Format DateTime Source Code Example
"I tried to learn .NET by taking boot camp classes that cost me $6,000. After taking the classes, I discovered tons of free (and very cheap) videos on the internet created by experts in the field. I found the videos to be much better than the classrooms. The videos were presented by Microsoft presenters that knew the material backwards and forwards, and they were much cheaper and better than the classroom teachers. I have spent the last three years organizing and hunting down the videos for my own use and decided that all could benefit by sharing what I found." - Wade Harvey
Click Here for Free 24 hour pass to lynda.com.
I AM VERY EXCITED ABOUT THIS AMAZING NEW DISCOVERY
Over 40,000 Top-Notch Video Tutorials (or 3,452 hours) on Lynda.com
I had stumbled across Lynda.com many times before, but never stopped to try it out. I recently took the plunge and started using Lynda.com to help me improve my javascript skills (See Javascript Essential Training 2007 by Dori Smith). It has revolutionized my understanding of the language! I love the fact that the exercise files allow you to follow along in real time with what the instructor is saying. Learning by doing is an extremely powerful technique. I have to give the site an A+ for both quality and content.
I am planning on using Lynda Videos to help me improve in:

Looking for premium .NET Training Videos? The best premium .NET Videos that I have found are at Learn Visual Studio. Those videos are only about 50 cents per hour, as compared to $25 per hour that other sites charge!
LearnVisualStudio is currently having a 25% off sale ($50 savings) and giving away a free membership to TrainingSpot ($60 value) when LVS Lifetime Membership is purchased.


Limited Time Offer: Free Lifetime Membership to Progress Monitor ($24.95 value) when you purchase any membership at Learn Visual Studio, Lynda Videos or any hosting account at DiscountASP - just forward order confirmation to harvey007@sbcglobal.net
I had stumbled across Lynda.com many times before, but never stopped to try it out. I recently took the plunge and started using Lynda.com to help me improve my javascript skills (See Javascript Essential Training 2007 by Dori Smith). It has revolutionized my understanding of the language! I love the fact that the exercise files allow you to follow along in real time with what the instructor is saying. Learning by doing is an extremely powerful technique. I have to give the site an A+ for both quality and content.
I am planning on using Lynda Videos to help me improve in:
- HTML
- CSS
- Classic ASP
- .NET (included in ASP section)
- AJAX
- Silverlight (in Microsoft Section)
- SharePoint (in Microsoft Section)
- Web Design
- Blogging
- SEO
- and much more

Looking for premium .NET Training Videos? The best premium .NET Videos that I have found are at Learn Visual Studio. Those videos are only about 50 cents per hour, as compared to $25 per hour that other sites charge!
LearnVisualStudio is currently having a 25% off sale ($50 savings) and giving away a free membership to TrainingSpot ($60 value) when LVS Lifetime Membership is purchased.


Limited Time Offer: Free Lifetime Membership to Progress Monitor ($24.95 value) when you purchase any membership at Learn Visual Studio, Lynda Videos or any hosting account at DiscountASP - just forward order confirmation to harvey007@sbcglobal.net
String.Format DateTime
Purpose: – Illustrates using String Format DateTime in C-Sharp ASP.NET.
Prerequistes:
- Install Visual Web Developer 2008
- Install SQL Server Express
- Download Northwind and Pubs Databases
- Attach Northwind Database to Databases in Sql Express
- Attach pubs Database to Databases in Sql Express
Notes:
- You can build your own library of syntax examples by using same web site over and over and just add new web forms to it.
Instructions:
- Use Visual Web Developer 2008
- Create new web site;
- Click File/New Web Site
- Select ASP.NET Website Template
- Select C-Sharp for Language
- name of Web Site could be CSharp_ASPNET_Syntax.
- Add New folder named "LanguageBasics"
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: LanguageBasics
- Add Web Form Named StingFormatDateTime to LanguageBasics folder
- Right-click LanguageBasics folder;
- add new item;
- Select Web Form
- Check place code behind in separate file
- Web Form name could be StingFormatDateTime
- Click on copy code in code below to copy code into web form StingFormatDateTime.aspx
- Click on copy code in second set of code below to copy code into code-behind StingFormatDateTime.aspx.cs
- Right-click on StingFormatDateTime.aspx in solution explorer and select view in browser
Step 1: Click on Copy Code to Cut-n-paste code into StingFormatDateTime.aspx
| XML | | copy code | | ? |
| 01 | |
| 02 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="StringFormatDateTime.aspx.cs" Inherits="LanguageBasics_StringFormatDateTime" %> |
| 03 | |
| 04 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 05 | |
| 06 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 07 | <head runat="server"> |
| 08 | <title></title> |
| 09 | </head> |
| 10 | <body> |
| 11 | <form id="form1" runat="server"> |
| 12 | <div> |
| 13 | <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> |
| 14 | </div> |
| 15 | </form> |
| 16 | </body> |
| 17 | </html> |
| 18 | |
| 19 | |
| 20 |
Step 2: Click on Copy Code to Cut-n-paste code into StringFormatDateTime.aspx.cs
| | | copy code | | ? |
| 001 | |
| 002 | using System; |
| 003 | using System.Text; |
| 004 | partial class LanguageBasics_StringFormatDateTime : System.Web.UI.Page |
| 005 | { |
| 006 | |
| 007 | |
| 008 | |
| 009 | protected void Page_Load(object sender, System.EventArgs e) |
| 010 | { |
| 011 | |
| 012 | //************************************************** |
| 013 | // Custom DateTime Formatting |
| 014 | //************************************************** |
| 015 | StringBuilder sb = new StringBuilder(); |
| 016 | sb.Append("Custom DateTime Formatting:<br />"); |
| 017 | |
| 018 | //Here are custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), |
| 019 | // f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone). |
| 020 | |
| 021 | //Following examples show how the format specifiers can be used. |
| 022 | // create date time 2009-12-21 20:04:08.101 |
| 023 | DateTime dt = new DateTime(2009, 12, 20, 20, 4, 8, 101); |
| 024 | |
| 025 | // Ways to format year: "9 09 009 2009" year |
| 026 | sb.Append(string.Format("{0:y yy yyy yyyy}", dt) + "<br />"); |
| 027 | |
| 028 | // Ways to format month: "12 12 Dec December" month |
| 029 | sb.Append(string.Format("{0:M MM MMM MMMM}", dt) + "<br />"); |
| 030 | |
| 031 | // Ways to format day: "21 21 Sun Sunday" day |
| 032 | sb.Append(string.Format("{0:d dd ddd dddd}", dt) + "<br />"); |
| 033 | |
| 034 | // Ways to format hour: "8 08 20 20" hour 12/24 |
| 035 | sb.Append(string.Format("{0:h hh H HH}", dt) + "<br />"); |
| 036 | |
| 037 | // Ways to format minute: "4 04" minute |
| 038 | sb.Append(string.Format("{0:m mm}", dt) + "<br />"); |
| 039 | |
| 040 | // Ways to format second: ' "8 08" second |
| 041 | sb.Append(string.Format("{0:s ss}", dt) + "<br />"); |
| 042 | |
| 043 | // Ways to format fraction of second: "1 10 101 1010" sec.fraction |
| 044 | sb.Append(string.Format("{0:f ff fff ffff}", dt) + "<br />"); |
| 045 | |
| 046 | // Ways to format fraction of second: "1 1 101 101" without zeros |
| 047 | sb.Append(string.Format("{0:F FF FFF FFFF}", dt) + "<br />"); |
| 048 | |
| 049 | // How to display am or pm: "P PM" A.M. or P.M. |
| 050 | sb.Append(string.Format("{0:t tt}", dt) + "<br />"); |
| 051 | |
| 052 | // How to display timezone: "-6 -06 -06:00" time zone |
| 053 | sb.Append(string.Format("{0:z zz zzz}", dt) + "<br />"); |
| 054 | |
| 055 | |
| 056 | //************************************************** |
| 057 | // Using date separator / (slash) and time sepatator : (colon). |
| 058 | //************************************************** |
| 059 | sb.Append("<br />"); |
| 060 | sb.Append("Using date separator / (slash) and time sepatator : (colon):<br />"); |
| 061 | |
| 062 | //These characters will be rewritten to characters defined |
| 063 | //in the current DateTimeFormatInfo.DateSeparator and DateTimeFormatInfo.TimeSeparator. |
| 064 | |
| 065 | |
| 066 | // "20/12/09 20:04:08" - english (en-US) |
| 067 | sb.Append(string.Format("{0:d/M/yyyy HH:mm:ss}", dt) + "<br />"); |
| 068 | |
| 069 | // "20.12.2009 20:04:08" - german (de-DE) |
| 070 | // date separator in german culture is "." (so "/" changes to ".") |
| 071 | sb.Append(string.Format("{0:d/M/yyyy HH:mm:ss}", dt) + "<br />"); |
| 072 | |
| 073 | //************************************************** |
| 074 | // Here are some examples of custom date and time formatting: |
| 075 | //************************************************** |
| 076 | sb.Append("<br />"); |
| 077 | sb.Append("Here are some examples of custom date and time formatting:<br />"); |
| 078 | |
| 079 | // month/day numbers without/with leading zeroes |
| 080 | // "12/20/2009" |
| 081 | sb.Append(string.Format("{0:M/d/yyyy}", dt) + "<br />"); |
| 082 | |
| 083 | // "12/20/2009" |
| 084 | sb.Append(string.Format("{0:MM/dd/yyyy}", dt) + "<br />"); |
| 085 | |
| 086 | // day/month names |
| 087 | // "Sun, Dec 20, 2009" |
| 088 | sb.Append(string.Format("{0:ddd, MMM d, yyyy}", dt) + "<br />"); |
| 089 | |
| 090 | // "Sunday, December 20, 2009" |
| 091 | // two/four digit year |
| 092 | sb.Append(string.Format("{0:dddd, MMMM d, yyyy}", dt) + "<br />"); |
| 093 | |
| 094 | // "12/20/09" |
| 095 | sb.Append(string.Format("{0:MM/dd/yy}", dt) + "<br />"); |
| 096 | |
| 097 | // "12/20/2009" |
| 098 | sb.Append(string.Format("{0:MM/dd/yyyy}", dt) + "<br />"); |
| 099 | |
| 100 | //************************************************** |
| 101 | // Standard DateTime Formatting |
| 102 | //************************************************** |
| 103 | sb.Append("<br />"); |
| 104 | sb.Append("Standard DateTime Formatting:<br />"); |
| 105 | //In DateTimeFormatInfo there are defined standard patterns for the current culture. |
| 106 | //For example, property ShortTimePattern is string that contains value h:mm tt for en-US culture |
| 107 | //and value HH:mm for de-DE culture. |
| 108 | |
| 109 | //Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. |
| 110 | //First column contains format specifiers for the String.Format method. |
| 111 | |
| 112 | //Specifier DateTimeFormatInfo property Pattern value (for en-US culture) |
| 113 | //t ShortTimePattern h:mm tt |
| 114 | //d ShortDatePattern M/d/yyyy |
| 115 | //T LongTimePattern h:mm:ss tt |
| 116 | //D LongDatePattern dddd, MMMM dd, yyyy |
| 117 | //f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt |
| 118 | //F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt |
| 119 | //g (combination of d and t) M/d/yyyy h:mm tt |
| 120 | //G (combination of d and T) M/d/yyyy h:mm:ss tt |
| 121 | //m, M MonthDayPattern MMMM dd |
| 122 | //y, Y YearMonthPattern MMMM, yyyy |
| 123 | //r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) |
| 124 | //s SortableDateTimePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) |
| 125 | //u UniversalSortableDateTimePattern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) |
| 126 | // (*) = culture independent |
| 127 | |
| 128 | //Following examples show usage of standard format specifiers in String.Format method and the resulting output. |
| 129 | |
| 130 | // "8:04 PM" ShortTime |
| 131 | sb.Append(string.Format("{0:t}", dt) + "<br />"); |
| 132 | |
| 133 | // "12/20/2009" ShortDate |
| 134 | sb.Append(string.Format("{0:d}", dt) + "<br />"); |
| 135 | |
| 136 | // "8:04:08 PM" LongTime |
| 137 | sb.Append(string.Format("{0:T}", dt) + "<br />"); |
| 138 | |
| 139 | // "Sunday, December 20, 2009" LongDate |
| 140 | sb.Append(string.Format("{0:D}", dt) + "<br />"); |
| 141 | |
| 142 | // "Sunday, December 20, 2009 8:04 PM" LongDate+ShortTime |
| 143 | sb.Append(string.Format("{0:f}", dt) + "<br />"); |
| 144 | |
| 145 | // "Sunday, December 20, 2009 8:04:08 PM" FullDateTime |
| 146 | sb.Append(string.Format("{0:F}", dt) + "<br />"); |
| 147 | |
| 148 | // "12/20/2009 8:04 PM" ShortDate+ShortTime |
| 149 | sb.Append(string.Format("{0:g}", dt) + "<br />"); |
| 150 | |
| 151 | // "12/20/2009 8:04:08 PM" ShortDate+LongTime |
| 152 | sb.Append(string.Format("{0:G}", dt) + "<br />"); |
| 153 | |
| 154 | // "December 20" MonthDay |
| 155 | sb.Append(string.Format("{0:m}", dt) + "<br />"); |
| 156 | |
| 157 | // "December, 2009" YearMonth |
| 158 | sb.Append(string.Format("{0:y}", dt) + "<br />"); |
| 159 | |
| 160 | // "Sun, 20 Dec 2009 20:04:08 GMT" |
| 161 | sb.Append(string.Format("{0:r}", dt) + "<br />"); |
| 162 | |
| 163 | // "2009-12-20T20:04:08" SortableDateTime |
| 164 | sb.Append(string.Format("{0:s}", dt) + "<br />"); |
| 165 | |
| 166 | // "2009-12-20 20:04:08Z" UniversalSortableDateTime |
| 167 | sb.Append(string.Format("{0:u}", dt) + "<br />"); |
| 168 | |
| 169 | Label1.Text = sb.ToString(); |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 |
Step 3: Click on Copy Code to Cut-n-paste code into web.config right after the appSettings section
| XML | | copy code | | ? |
| 1 | |
| 2 | <connectionStrings> |
| 3 | <add name="Northwind_ConnectionString" |
| 4 | connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> |
| 5 | <add name="Pubs_ConnectionString" |
| 6 | connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> |
| 7 | </connectionStrings> |
| 8 | |
| 9 |
Related posts:
- VB.NET ASP.NET String.Format DateTime Source Code Example VB.NET ASP.NET String.Format DateTime Source Code Example...
- C# String.Format DateTime Source Code Example C# String.Format DateTime Source Code Example...
- VB.NET String Format DateTime Source Code Example VB.NET String Format DateTime Source Code Example...
- C# ASP.NET StringBuilder Source Code Example C# ASP.NET StringBuilder Source Code Example...
- C# StringBuilder Source Code Example C# StringBuilder Source Code Example...
Related posts brought to you by Yet Another Related Posts Plugin.

hi,
First of all. Thanks very much for your useful post.
I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.
Please let me introduce you some info related to this post and I hope that it is useful for .Net community.
There is a good C# resource site, Have alook
http://csharptalk.com
simi