Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
String format for DateTime in C#
The String.Format method in C# provides powerful formatting capabilities for DateTime objects. You can use various format specifiers to display dates and times in different formats, from simple year displays to complex custom patterns.
Syntax
Following is the syntax for formatting DateTime using String.Format −
String.Format("{0:format_specifier}", dateTimeObject)
Common format specifiers include −
y− Year (1 digit minimum)yy− Year (2 digits)yyyy− Year (4 digits)M− Month (1-12)MM− Month (01-12)MMM− Month abbreviation (Jan, Feb, etc.)MMMM− Full month name (January, February, etc.)d− Day (1-31)dd− Day (01-31)ddd− Day abbreviation (Sun, Mon, etc.)dddd− Full day name (Sunday, Monday, etc.)
Using Date Format Specifiers
Example
using System;
static class Demo {
static void Main() {
DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);
Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d));
Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d));
Console.WriteLine(String.Format("{0:d dd ddd dddd}", d));
}
}
The output of the above code is −
18 18 2018 2018 2 02 Feb February 8 08 Thu Thursday
Using Time Format Specifiers
Example
using System;
class Program {
static void Main() {
DateTime dt = new DateTime(2018, 2, 8, 15, 30, 45, 123);
Console.WriteLine("Hour formats:");
Console.WriteLine(String.Format("{0:h hh H HH}", dt));
Console.WriteLine("Minute and Second formats:");
Console.WriteLine(String.Format("{0:m mm s ss}", dt));
Console.WriteLine("AM/PM and Milliseconds:");
Console.WriteLine(String.Format("{0:tt fff}", dt));
}
}
The output of the above code is −
Hour formats: 3 03 15 15 Minute and Second formats: 30 30 45 45 AM/PM and Milliseconds: PM 123
Using Standard Format Strings
Example
using System;
class Program {
static void Main() {
DateTime now = DateTime.Now;
Console.WriteLine("Standard date formats:");
Console.WriteLine(String.Format("Short date: {0:d}", now));
Console.WriteLine(String.Format("Long date: {0:D}", now));
Console.WriteLine(String.Format("Full date/time: {0:F}", now));
Console.WriteLine(String.Format("General: {0:G}", now));
Console.WriteLine(String.Format("Short time: {0:t}", now));
Console.WriteLine(String.Format("Long time: {0:T}", now));
}
}
The output of the above code is −
Standard date formats: Short date: 12/25/2023 Long date: Monday, December 25, 2023 Full date/time: Monday, December 25, 2023 10:30:15 AM General: 12/25/2023 10:30:15 AM Short time: 10:30 AM Long time: 10:30:15 AM
Custom Format Combinations
Example
using System;
class Program {
static void Main() {
DateTime dt = new DateTime(2018, 2, 8, 15, 30, 45);
Console.WriteLine("Custom combinations:");
Console.WriteLine(String.Format("Date: {0:MMMM dd, yyyy}", dt));
Console.WriteLine(String.Format("Time: {0:hh:mm:ss tt}", dt));
Console.WriteLine(String.Format("Full: {0:dddd, MMMM dd, yyyy 'at' h:mm tt}", dt));
Console.WriteLine(String.Format("ISO format: {0:yyyy-MM-dd HH:mm:ss}", dt));
}
}
The output of the above code is −
Custom combinations: Date: February 08, 2018 Time: 03:30:45 PM Full: Thursday, February 08, 2018 at 3:30 PM ISO format: 2018-02-08 15:30:45
Conclusion
String.Format provides extensive DateTime formatting capabilities in C#. You can use single-character format specifiers like y, M, d for basic formatting, standard format strings like d, D, F for common patterns, or create custom combinations to display dates and times exactly as needed.
