C# 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:

  1. HTML
  2. CSS
  3. Classic ASP
  4. .NET (included in ASP section)
  5. AJAX
  6. Silverlight (in Microsoft Section)
  7. SharePoint (in Microsoft Section)
  8. Web Design
  9. Blogging
  10. SEO
  11. and much more
You can learn more about Lynda.com by watching the 5-minute video at About Lynda.com. If you are only focusing on .NET, you are missing out on the "big picture" and on many of the underlying fundamentals! To be an Ideal Programmer, you need to have the largest knowledge base possible.

JavaScript tutorials



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.

  

DiscountASP .NET hosting  JavaScript tutorials


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 in C-Sharp.



Prerequistes:

  1. Install C# (Express or Standard Edition)
  2. Install SQL Server Express
  3. Download Northwind and Pubs Databases
  4. Attach Northwind Database to Databases in Sql Express
  5. Attach pubs Database to Databases in Sql Express

Notes:

  • You can build your own library of syntax examples by using same project over and over and just adding new classes to it.

Instructions:

  1. Use C# 2008 (express or standard)
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select C-Sharp for Language
    • name of project could be CSharp_Syntax.
  3. Add New folder named "LanguageBasics"
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: LanguageBasics
  4. Add Class named clsStringFormatDateTime to LanguageBasics folder
    • Right-click LanguageBasics folder;
    • add new item;
    • Select Class
    • Class name could be clsStringFormatDateTime
  5. Click on copy code in code below to copy code into class clsStringFormatDateTime.cs
  6. Click on copy code in second set of code below to copy code into Program.cs
  7. Click on green arrow in toolbar in VS or press F5 to run program.

Step 1: Click on Copy Code to Cut-n-paste code into clsStringFormatDateTime.cs

  |  copy code |? 
001
002
using System;
003
 
004
public class clsStringFormatDateTime
005
{
006
 
007
    public void Main()
008
    {
009
        //**************************************************
010
        //        Custom DateTime Formatting
011
        //**************************************************
012
 
013
        Console.WriteLine("Custom DateTime Formatting:");
014
        //Here are custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second),
015
        // f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).
016
 
017
        //Following examples show how the format specifiers can be used.
018
        // create date time 2009-12-21 20:04:08.101
019
        DateTime dt = new DateTime(2009, 12, 20, 20, 4, 8, 101);
020
 
021
        // Ways to format year: "9 09 009 2009"   year
022
        Console.WriteLine(string.Format("{0:y yy yyy yyyy}", dt));
023
 
024
        // Ways to format month: "12 12 Dec December"  month
025
        Console.WriteLine(string.Format("{0:M MM MMM MMMM}", dt));
026
 
027
        // Ways to format day: "21 21 Sun Sunday" day
028
        Console.WriteLine(string.Format("{0:d dd ddd dddd}", dt));
029
 
030
        // Ways to format hour: "8 08 20 20"      hour 12/24
031
        Console.WriteLine(string.Format("{0:h hh H HH}", dt));
032
 
033
        // Ways to format minute: "4 04"            minute
034
        Console.WriteLine(string.Format("{0:m mm}", dt));
035
 
036
        // Ways to format second:  ' "8 08"            second 
037
        Console.WriteLine(string.Format("{0:s ss}", dt));
038
 
039
        // Ways to format fraction of second: "1 10 101 1010"   sec.fraction
040
        Console.WriteLine(string.Format("{0:f ff fff ffff}", dt));
041
 
042
        // Ways to format fraction of second: "1 1 101 101"   without zeros
043
        Console.WriteLine(string.Format("{0:F FF FFF FFFF}", dt));
044
 
045
        // How to display am or pm: "P PM"            A.M. or P.M.
046
        Console.WriteLine(string.Format("{0:t tt}", dt));
047
 
048
        // How to display timezone: "-6 -06 -06:00"   time zone
049
        Console.WriteLine(string.Format("{0:z zz zzz}", dt));
050
 
051
 
052
        //**************************************************
053
        //        Using date separator / (slash) and time sepatator : (colon).
054
        //**************************************************
055
        Console.WriteLine("\n");
056
        Console.WriteLine("Using date separator / (slash) and time sepatator : (colon):");
057
 
058
        //These characters will be rewritten to characters defined 
059
        //in the current DateTimeFormatInfo.DateSepa­rator and DateTimeFormatInfo.TimeSeparator.
060
 
061
 
062
        // "20/12/09 20:04:08" - english (en-US)
063
        Console.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss}", dt));
064
 
065
        // "20.12.2009 20:04:08" - german (de-DE)
066
        // date separator in german culture is "." (so "/" changes to ".")
067
        Console.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss}", dt));
068
 
069
        //**************************************************
070
        // Here are some examples of custom date and time formatting:
071
        //**************************************************
072
        Console.WriteLine("\n");
073
        Console.WriteLine("Here are some examples of custom date and time formatting:");
074
 
075
        // month/day numbers without/with leading zeroes
076
        // "12/20/2009"
077
        Console.WriteLine(string.Format("{0:M/d/yyyy}", dt));
078
 
079
        // "12/20/2009"
080
        Console.WriteLine(string.Format("{0:MM/dd/yyyy}", dt));
081
 
082
        // day/month names
083
        // "Sun, Dec 20, 2009"
084
        Console.WriteLine(string.Format("{0:ddd, MMM d, yyyy}", dt));
085
 
086
        // "Sunday, December 20, 2009"
087
        // two/four digit year
088
        Console.WriteLine(string.Format("{0:dddd, MMMM d, yyyy}", dt));
089
 
090
        // "12/20/09"
091
        Console.WriteLine(string.Format("{0:MM/dd/yy}", dt));
092
 
093
        // "12/20/2009"
094
        Console.WriteLine(string.Format("{0:MM/dd/yyyy}", dt));
095
 
096
        //**************************************************
097
        //        Standard DateTime Formatting
098
        //**************************************************
099
        Console.WriteLine("\n");
100
        Console.WriteLine("Standard DateTime Formatting:");
101
        //In DateTimeFormatInfo there are defined standard patterns for the current culture. 
102
        //For example, property ShortTimePattern is string that contains value h:mm tt for en-US culture 
103
        //and value HH:mm for de-DE culture.
104
 
105
        //Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. 
106
        //First column contains format specifiers for the String.Format method.
107
 
108
        //Specifier DateTimeFormatInfo property Pattern value (for en-US culture) 
109
        //t ShortTimePattern h:mm tt 
110
        //d ShortDatePattern M/d/yyyy 
111
        //T LongTimePattern h:mm:ss tt 
112
        //D LongDatePattern dddd, MMMM dd, yyyy 
113
        //f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt 
114
        //F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt 
115
        //g (combination of d and t) M/d/yyyy h:mm tt 
116
        //G (combination of d and T) M/d/yyyy h:mm:ss tt 
117
        //m, M MonthDayPattern MMMM dd 
118
        //y, Y YearMonthPattern MMMM, yyyy 
119
        //r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) 
120
        //s SortableDateTi­mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) 
121
        //u UniversalSorta­bleDateTimePat­tern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) 
122
        //    (*) = culture independent 
123
 
124
        //Following examples show usage of standard format specifiers in String.Format method and the resulting output.
125
 
126
        // "8:04 PM"                         ShortTime
127
        Console.WriteLine(string.Format("{0:t}", dt));
128
 
129
        // "12/20/2009"                        ShortDate
130
        Console.WriteLine(string.Format("{0:d}", dt));
131
 
132
        // "8:04:08 PM"                      LongTime
133
        Console.WriteLine(string.Format("{0:T}", dt));
134
 
135
        // "Sunday, December 20, 2009"          LongDate
136
        Console.WriteLine(string.Format("{0:D}", dt));
137
 
138
        // "Sunday, December 20, 2009 8:04 PM"  LongDate+ShortTime
139
        Console.WriteLine(string.Format("{0:f}", dt));
140
 
141
        // "Sunday, December 20, 2009 8:04:08 PM" FullDateTime
142
        Console.WriteLine(string.Format("{0:F}", dt));
143
 
144
        // "12/20/2009 8:04 PM"                ShortDate+ShortTime
145
        Console.WriteLine(string.Format("{0:g}", dt));
146
 
147
        // "12/20/2009 8:04:08 PM"             ShortDate+LongTime
148
        Console.WriteLine(string.Format("{0:G}", dt));
149
 
150
        // "December 20"                        MonthDay
151
        Console.WriteLine(string.Format("{0:m}", dt));
152
 
153
        // "December, 2009"                     YearMonth
154
        Console.WriteLine(string.Format("{0:y}", dt));
155
 
156
        // "Sun, 20 Dec 2009 20:04:08 GMT"   
157
        Console.WriteLine(string.Format("{0:r}", dt));
158
 
159
        // "2009-12-20T20:04:08"             SortableDateTime
160
        Console.WriteLine(string.Format("{0:s}", dt));
161
 
162
        // "2009-12-20 20:04:08Z"            UniversalSortableDateTime
163
        Console.WriteLine(string.Format("{0:u}", dt));
164
 
165
 
166
 
167
        Console.ReadLine();
168
    }
169
}
170
 
171
 
172
 
173
 
174
 
175


Step 2: Click on Copy Code to Cut-n-paste code into Program.cs
  |  copy code |? 
01
02
using System;
03
using System.Collections.Generic;
04
using System.Linq;
05
using System.Text;
06
 
07
namespace CSharp_Syntax
08
{
09
    class Program
10
    {
11
        static void Main(string[] args)
12
        {
13
            //LanguageBasics
14
 
15
 
16
            clsStringFormatDateTime myStringFormatDateTime = new clsStringFormatDateTime();
17
            myStringFormatDateTime.Main();
18
 
19
 
20
        }
21
    }
22
}
23
 
24
 
25
 
26



Step 3: Click on green arrow in toolbar in VS or press F5 to run program.

Related posts:

  1. VB.NET String Format DateTime Source Code Example VB.NET String Format DateTime Source Code Example...
  2. C# ASP.NET String.Format DateTime Source Code Example C# ASP.NET String.Format DateTime Source Code Example...
  3. VB.NET ASP.NET String.Format DateTime Source Code Example VB.NET ASP.NET String.Format DateTime Source Code Example...
  4. C# StringBuilder Source Code Example C# StringBuilder Source Code Example...
  5. VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...

Related posts brought to you by Yet Another Related Posts Plugin.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

This blog uses the cross-linker plugin developed by Web-Developers.Net

http://idealprogrammer.com/home/idearcom/public_html/wp-content/plugins/wp-super-cache/