How to print the year of a date in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        DateTime now = DateTime.Now;
        
        Console.WriteLine(now.ToString("y "));
        Console.WriteLine(now.ToString("yy"));
        Console.WriteLine(now.ToString("yyy"));
        Console.WriteLine(now.ToString("yyyy"));
    }
}




/*
run:

20 
20
2020
2020

*/

 



answered Sep 12, 2020 by avibootz
...