Get Current Year in C#

Syed Hassan Sabeeh Kazmi Jan 30, 2023 Nov 21, 2022
  1. Use the DateTime.Year Property to Get the Current Year in C#
  2. Use the DateTime.Now Property to Get the Current Year in C#
  3. Use the DateTime.Today Property to Get the Current Year in C#
  4. Use the DateTime.UtcNow Property to Get the Current Year in C#
Get Current Year in C#

This tutorial will discuss how to get the current year in C# programatically at run time. One way is to get the Year component from the DateTime object using the DateTime.Year property of the .Net framework.

The System namespace features the DateTime property, which returns the System.Int32 data type. You can use DateTime.Now, DateTime.Today, DateTime.Year, or DateTime.UtcNow to get the current year in C#.

Use the DateTime.Year Property to Get the Current Year in C#

It returns the year of the DateTime instance in the Gregorian Calender by creating an object from the DateTime property. It gets the year component of the date represented by its instance, and to retrieve the year using a particular calendar, you must call the GetYear method and use the System.Globalization namespace.

Any valid object from the DateTime structure can give you the current year in default value 01/01/0001 00:00:00 that can be customized. You can overload the DateTime struct to ease out the date operations, and the parse() methods can be used to convert a valid string into a DateTime object.

using System;
using System.Windows.Forms;

namespace current_year
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime current_year = DateTime.Now;
            string year = current_year.Year.ToString();
            label1.Text = year;
        }
    }
}

Output:

Use the DateTime.Year Property to Get the Current Year

The DateTime object offers different properties to help you retrieve data about the set year of the date-time object. Furthermore, different specifiers are available in C# designated to offer you the desired format of the date, including; short date, long date, and month/year.

Use the DateTime.Now Property to Get the Current Year in C#

It returns a DateTime object that contains both year and month properties as integers from the system’s current date. You can use something similar to string curr_date = DateTime.Now.ToString(); or more specifically, string cur_year = DateTime.Now.Year.ToString(); to retrieve the current year from the system in C#.

However, there’s an issue with this approach. What if the first operation executes on December 31 at 23.59.59.999, which is rarest but still the possibility which leads to the execution of the second operation on January 1 already, which will give you the current month as December and the current year as the new year number which is incorrect. In this case, you can store the DateTime in a variable and execute the operations later like DateTime _var = DateTime.Now; and can run operations like string cur_year = _var.Year.ToString();.

To set the format of your string value containing the month and year from the DateTime property, use the DateTime.Now.ToString("MMMM yyyy") approach or customize it according to your needs.

using System;
using System.Windows.Forms;

namespace current_year
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.Year.ToString();
        }
    }
}

Output:

Year: 2022
Image: https://asset.cloudinary.com/dicdyo83o/292bd34e83db5bfa4362da920ace5bf9

Use the DateTime.Today Property to Get the Current Year in C#

Using the DateTime.Today property gives you the current year in the yyyy format. As the DateTime property belongs to the System, you can get any year from 0001 Anno Domini or common era to December 32 9999 A.D. (C.E.) in the Gregorian calendar.

It represents an instant in time and provides various aspects of the date and time as you can create an object of the DateTime property and set it to the current local date by doing Console.WriteLine("Today's date: {0}", obj.Today); // or year component "obj.Year". It’s possible to add and subtract years in DateTime by doing something like obj.AddYears(4);.

A date format string defines the text representation of a date results from a formatting operation which can be either standard or custom and consists of two or more characters. You can use d for a short date pattern, D for a long date pattern, and F for a full date and time pattern in obj.ToString("here...");.

using System;
using System.Windows.Forms;

namespace current_year
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime _current = DateTime.Today;
            label1.Text = _current.ToString("yyyy");
        }
    }
}

Output:

Year: 2022
Image: https://asset.cloudinary.com/dicdyo83o/23f8f56538087e8ac05de862ddcf36eb

Using the Parse method is another approach to convert the string representation of a date or current year to its DateTime equivalent, and the date is printed to the console in a middle-endian order.

Use the DateTime.UtcNow Property to Get the Current Year in C#

Each time zone has a different local time and is often further modified by daylight saving to avoid confusion about time zones; that’s where UTC comes to the rescue, and by using DateTime.UtcNow, you can get the universal coordinated time and year in C#. The UTC is local time plus the bias (the difference between UTC time and local time) that you can code by scratch or use the pre-defined System’s DateTime.UtcNow property.

Formates like ToSting("o") can retrieve timezone information from a DateTime value and is an equivalent approach to using this method. As a struct, the DateTime is more like an int than a class instance as it produces only the year in your case, and its constructor is intelligent enough to validate possible arguments.

using System;
using System.Windows.Forms;

namespace current_year
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime _current = DateTime.UtcNow;
            label1.Text = _current.ToString("yyyy");
        }
    }
}

Output:

Year: 2022
Image: https://asset.cloudinary.com/dicdyo83o/e47d35e37f20469ef375e2f9c2ca86cf

Always remember that the UtcNow property represents the Greenwich MeanTime and does not depends on the time zone of the current computer configuration. Optimizing your DateTime cache with this method is possible as it prevents excessive time queries.

In this tutorial, you have learned the possible ways to get the current year in C# for satisfying your various purposes. A DateTime object can gather information about the date and time of the system or set a custom date for a particular C# application requirement.

Syed Hassan Sabeeh Kazmi avatar Syed Hassan Sabeeh Kazmi avatar

Hassan is a Software Engineer with a well-developed set of programming skills. He uses his knowledge and writing capabilities to produce interesting-to-read technical articles.

GitHub

Related Article - Csharp DateTime