C# FormatDateTime Example | FormatDateTime | Source Code

Get 5 Hours of FREE PREMIUM Videos:

LearnVisualStudio.NET Free Preview


LearnVisualStudio.NET Free Preview: Declaring Variables and Assigning Values

I am a lifetime member of LearnVisualStudio.net and a Premium Plus member of dotNetVideos.net.

LearnVisualStudio.net is awesome because it grows in value each year as more videos are added.

dotNetVideos.net is also great because it focuses a lot on MS Certifications and practical interview questions.


- Wade Harvey (IdealProgrammer.com)

LearnVisualStudio Premium Videos

Visual C# 2010 for Absolute Beginners C# for Absolute Beginners C# for Absolute Beginners Study Guide Visual Basic for Absolute Beginners Visual Basic Express Edition Study Guide
Visual Basic 9.0 Language Enhancements Entity Framework Linq To Sql Windows Phone 7 Development Unit Testing
ASP.NET MVC 3 (In Progress) ASP.NET MVC Hands On Project ASP.NET For Absolute Beginners Visual Web Developer C# Study Guide Visual Web Developer VB.NET Study Guide
ASP.NET Controls Series ASP.NET AJAX ASP.NET Architecture Series ASP.NET Server Controls Silverlight 4.0
Windows Presentation Foundation Windows Forms Controls Visual Studio 2010 New Features Visual Studio Team System 2008 Visual Studio Team System Features
Getting Started With Sql Server Express Edition


dotNetVideos Premium Video Catalog

Interview Questions & Answers Windows AzureASP.NET MVC 4Windows Phone 7 (3 Videos)Visual Studio 2011 Application Lifecycle Management
LightSwitchHTML 5SOLIDVisual Studio Add-OnsVisual Studio 2010 ALM Lab Management
ASP.NET 4 Deep Dive Microsoft Enterprise Library 5 with ASP.NET 4 (10 Videos)Virtualization Techniques for Developer (5 Videos)Test Series - 70-519: Designing and Developing Web Applications Using Microsoft .NET Framework 4 (20+ Videos)
Test Series - 70-519: Supplemental70-513: Test Series (MCTS) Windows Communication Foundation 4 (20+ Videos)70-515:Web Applications Development with Microsoft .NET Framework 4 (20+ Videos)Test Series - 70-516: Accessing Data with Microsoft .NET Framework 4 (20+ Videos)Test Series: 70-432: Microsoft SQL Server 2008, Implementation and Maintenance (20+ Videos)
Test Series: 70-433: SQL Server AdministrationFrom Novice To Professional - C# (25 Videos)From Novice To Professional - VB.NET (25 Videos)




Premium (Not Free) Video Tutorials

Free Video Tutorials & Free Tools

C# FormatDateTime Example | FormatDateTime | Source Code

Purpose: – Illustrates the for .




Step 1: Click on C# to Cut-n-paste code into clsFormatDateTime.cs
 C# |  copy code |? 
using System;
 
public class clsFormatDateTime
{
 
    public void Main()
    {
        //**************************************************
        //        Custom DateTime Formatting
        //**************************************************
 
        Console.WriteLine("Custom DateTime Formatting:");
        //Here are custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second),
        // f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).
 
        //Following examples show how the format specifiers can be used.
        // create date time 2009-12-21 20:04:08.101
        DateTime dt = new DateTime(2009, 12, 20, 20, 4, 8, 101);
 
        // Ways to format year: "9 09 009 2009"   year
        Console.WriteLine(string.Format("{0:y yy yyy yyyy}", dt));
 
        // Ways to format month: "12 12 Dec December"  month
        Console.WriteLine(string.Format("{0:M MM MMM MMMM}", dt));
 
        // Ways to format day: "21 21 Sun Sunday" day
        Console.WriteLine(string.Format("{0:d dd ddd dddd}", dt));
 
        // Ways to format hour: "8 08 20 20"      hour 12/24
        Console.WriteLine(string.Format("{0:h hh H HH}", dt));
 
        // Ways to format minute: "4 04"            minute
        Console.WriteLine(string.Format("{0:m mm}", dt));
 
        // Ways to format second:  ' "8 08"            second 
        Console.WriteLine(string.Format("{0:s ss}", dt));
 
        // Ways to format fraction of second: "1 10 101 1010"   sec.fraction
        Console.WriteLine(string.Format("{0:f ff fff ffff}", dt));
 
        // Ways to format fraction of second: "1 1 101 101"   without zeros
        Console.WriteLine(string.Format("{0:F FF FFF FFFF}", dt));
 
        // How to display am or pm: "P PM"            A.M. or P.M.
        Console.WriteLine(string.Format("{0:t tt}", dt));
 
        // How to display timezone: "-6 -06 -06:00"   time zone
        Console.WriteLine(string.Format("{0:z zz zzz}", dt));
 
 
        //**************************************************
        //        Using date separator / (slash) and time sepatator : (colon).
        //**************************************************
        Console.WriteLine(Environment.NewLine);
        Console.WriteLine("Using date separator / (slash) and time sepatator : (colon):");
 
        //These characters will be rewritten to characters defined 
        //in the current DateTimeFormatInfo.DateSepa­rator and DateTimeFormatInfo.TimeSeparator.
 
 
        // "20/12/09 20:04:08" - english (en-US)
        Console.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss}", dt));
 
        // "20.12.2009 20:04:08" - german (de-DE)
        // date separator in german culture is "." (so "/" changes to ".")
        Console.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss}", dt));
 
        //**************************************************
        // Here are some examples of custom date and time formatting:
        //**************************************************
        Console.WriteLine(Environment.NewLine);
        Console.WriteLine("Here are some examples of custom date and time formatting:");
 
        // month/day numbers without/with leading zeroes
        // "12/20/2009"
        Console.WriteLine(string.Format("{0:M/d/yyyy}", dt));
 
        // "12/20/2009"
        Console.WriteLine(string.Format("{0:MM/dd/yyyy}", dt));
 
        // day/month names
        // "Sun, Dec 20, 2009"
        Console.WriteLine(string.Format("{0:ddd, MMM d, yyyy}", dt));
 
        // "Sunday, December 20, 2009"
        // two/four digit year
        Console.WriteLine(string.Format("{0:dddd, MMMM d, yyyy}", dt));
 
        // "12/20/09"
        Console.WriteLine(string.Format("{0:MM/dd/yy}", dt));
 
        // "12/20/2009"
        Console.WriteLine(string.Format("{0:MM/dd/yyyy}", dt));
 
        //**************************************************
        //        Standard DateTime Formatting
        //**************************************************
        Console.WriteLine(Environment.NewLine);
        Console.WriteLine("Standard DateTime Formatting:");
        //In DateTimeFormatInfo there are defined standard patterns for the current culture. 
        //For example, property ShortTimePattern is string that contains value h:mm tt for en-US culture 
        //and value HH:mm for de-DE culture.
 
        //Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. 
        //First column contains format specifiers for the String.Format method.
 
        //Specifier DateTimeFormatInfo property Pattern value (for en-US culture) 
        //t ShortTimePattern h:mm tt 
        //d ShortDatePattern M/d/yyyy 
        //T LongTimePattern h:mm:ss tt 
        //D LongDatePattern dddd, MMMM dd, yyyy 
        //f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt 
        //F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt 
        //g (combination of d and t) M/d/yyyy h:mm tt 
        //G (combination of d and T) M/d/yyyy h:mm:ss tt 
        //m, M MonthDayPattern MMMM dd 
        //y, Y YearMonthPattern MMMM, yyyy 
        //r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) 
        //s SortableDateTi­mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) 
        //u UniversalSorta­bleDateTimePat­tern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) 
        //    (*) = culture independent 
 
        //Following examples show usage of standard format specifiers in String.Format method and the resulting output.
 
        // "8:04 PM"                         ShortTime
        Console.WriteLine(string.Format("{0:t}", dt));
 
        // "12/20/2009"                        ShortDate
        Console.WriteLine(string.Format("{0:d}", dt));
 
        // "8:04:08 PM"                      LongTime
        Console.WriteLine(string.Format("{0:T}", dt));
 
        // "Sunday, December 20, 2009"          LongDate
        Console.WriteLine(string.Format("{0:D}", dt));
 
        // "Sunday, December 20, 2009 8:04 PM"  LongDate+ShortTime
        Console.WriteLine(string.Format("{0:f}", dt));
 
        // "Sunday, December 20, 2009 8:04:08 PM" FullDateTime
        Console.WriteLine(string.Format("{0:F}", dt));
 
        // "12/20/2009 8:04 PM"                ShortDate+ShortTime
        Console.WriteLine(string.Format("{0:g}", dt));
 
        // "12/20/2009 8:04:08 PM"             ShortDate+LongTime
        Console.WriteLine(string.Format("{0:G}", dt));
 
        // "December 20"                        MonthDay
        Console.WriteLine(string.Format("{0:m}", dt));
 
        // "December, 2009"                     YearMonth
        Console.WriteLine(string.Format("{0:y}", dt));
 
        // "Sun, 20 Dec 2009 20:04:08 GMT"   
        Console.WriteLine(string.Format("{0:r}", dt));
 
        // "2009-12-20T20:04:08"             SortableDateTime
        Console.WriteLine(string.Format("{0:s}", dt));
 
        // "2009-12-20 20:04:08Z"            UniversalSortableDateTime
        Console.WriteLine(string.Format("{0:u}", dt));
 
        Console.ReadLine();
    }
}
 

Step 2: Click on C# to Cut-n-paste code into Program.cs
 C# |  copy code |? 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace CSharp_Syntax
{
    class Program
    {
        static void Main(string[] args)
        {
 
 
            clsFormatDateTime myFormatDateTime = new clsFormatDateTime();
            myFormatDateTime.Main();
 
        }
    }
}
 
 
 

Prerequistes:

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

Notes:

  • Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
  • You can build a library of syntax examples by using same project over and over and just commenting out what you do not want to execute in Program.cs

Instructions:

  1. Use C# 2010 Express or Standard Edition
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select C# for Language
    • name of project could be CSharp_Syntax.
  3. CS Syntax File Directory Binary File Read Sequential

  4. Add New folder named "StringManipulation"
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: StringManipulation
  5. Add Class Named clsFormatDateTime to StringManipulation folder
    • Right-click StringManipulation folder;
    • add new item;
    • Select class
    • Class name could be clsFormatDateTime
  6. Click on C# in code in step 1 at top of page to copy code into clsFormatDateTime.cs
  7. Click on C# in step 2 at top of page to copy code into Program.cs
  8. Click green arrow or press F5 to run program

Related posts:

  1. VB.NET ASP.NET FormatDateTime() Source Code Syntax Example VB.NET ASP.NET FormatDateTime() Source Code Syntax Example...
  2. VB.NET FormatDateTime() – Code Sample Syntax Example VB.NET FormatDateTime() - Code Sample Syntax Example...
  3. C# String.Format DateTime Source Code Example C# String.Format DateTime Source Code Example...
  4. VB.NET String Format DateTime Source Code Example VB.NET String Format DateTime Source Code Example...
  5. C# ASP.NET String.Format DateTime Source Code Example C# ASP.NET String.Format DateTime 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!

Powered by WP Hashcash

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