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
Display years in different formats with C# DateTime
In C#, you can display years in different formats using the DateTime.ToString() method with specific year format specifiers. The DateTime class provides several format specifiers to control how the year is displayed.
Syntax
Following is the syntax for formatting years using DateTime.ToString() −
DateTime dt = DateTime.Now;
dt.ToString("format_specifier");
Year Format Specifiers
| Format Specifier | Description | Example Output |
|---|---|---|
| yy | Two-digit year (00-99) | 23 |
| yyy | Three-digit year with leading zeros | 2023 |
| yyyy | Four-digit year | 2023 |
| yyyyy | Five-digit year with leading zero | 02023 |
Using Different Year Formats
Example
using System;
public class Demo {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("Today = {0}", DateTime.Today);
Console.WriteLine("Displaying current year in different formats:");
Console.WriteLine("yy format: " + dt.ToString("yy"));
Console.WriteLine("yyy format: " + dt.ToString("yyy"));
Console.WriteLine("yyyy format: " + dt.ToString("yyyy"));
Console.WriteLine("yyyyy format: " + dt.ToString("yyyyy"));
}
}
The output of the above code is −
Today = 12/15/2023 12:00:00 AM Displaying current year in different formats: yy format: 23 yyy format: 2023 yyyy format: 2023 yyyyy format: 02023
Using Specific DateTime Values
Example
using System;
public class YearFormatsDemo {
public static void Main() {
DateTime dt1 = new DateTime(2023, 8, 15);
DateTime dt2 = new DateTime(2005, 12, 31);
DateTime dt3 = new DateTime(1995, 1, 1);
Console.WriteLine("Date: " + dt1.ToString("MM/dd/yyyy"));
Console.WriteLine("Year formats for " + dt1.Year + ":");
Console.WriteLine("yy: " + dt1.ToString("yy"));
Console.WriteLine("yyyy: " + dt1.ToString("yyyy"));
Console.WriteLine("yyyyy: " + dt1.ToString("yyyyy"));
Console.WriteLine("\nComparing different years:");
Console.WriteLine("2005 as yy: " + dt2.ToString("yy"));
Console.WriteLine("1995 as yy: " + dt3.ToString("yy"));
}
}
The output of the above code is −
Date: 08/15/2023 Year formats for 2023: yy: 23 yyyy: 2023 yyyyy: 02023 Comparing different years: 2005 as yy: 05 1995 as yy: 95
Common Use Cases
-
Two-digit year (yy): Used in abbreviated formats where space is limited, such as file naming or compact displays.
-
Four-digit year (yyyy): The most common format for displaying full years in applications and reports.
-
Five-digit year (yyyyy): Useful when you need consistent width formatting with leading zeros for alignment purposes.
Conclusion
C# provides flexible year formatting options through DateTime format specifiers. The most commonly used are yy for two-digit years and yyyy for four-digit years, allowing you to display dates according to your application's specific requirements.
