Do you want to get the first or last day of the year in C#? In this C# tutorial, I will explain to you how to get the first and last day of the year in C#.
I will also show you how to calculate the first and last day of the current year and display their day names (like “Sunday” or “Monday”).
Get the First Day of the Year in C#
The first day of any year is simply January 1st of that year. In C#, you can get the first day of the current year by using the DateTime
structure. Here’s how you can do it:
int currentYear = DateTime.Now.Year; // This gets the current year
DateTime firstDayOfYear = new DateTime(currentYear, 1, 1); // January 1st of the current year
This code uses DateTime.Now.Year
to find out the current year and then creates a new DateTime
object representing January 1st of that year.
Get Last Day of the Year in C#
The last day of any year is December 31st of that year. Similar to getting the first day, you can get the last day of the year like this:
int currentYear = DateTime.Now.Year; // This gets the current year
DateTime lastDayOfYear = new DateTime(currentYear, 12, 31); // December 31st of the current year
Here, we again use DateTime.Now.Year
to get the current year and then instantiate a new DateTime
object for December 31st of the current year.
Practical Usage
Knowing how to get the first and last day of the year can be useful in many scenarios, such as generating reports for the year, calculating annual fees, or setting up schedules and reminders for year-end activities.
Formatting the Dates
Often, you’ll want to display these dates in a more readable format. C# provides powerful formatting options for DateTime
objects. For example:
string formattedFirstDay = firstDayOfYear.ToString("MMMM dd, yyyy"); // "January 01, 2023"
string formattedLastDay = lastDayOfYear.ToString("MMMM dd, yyyy"); // "December 31, 2023"
The ToString
method with a format string allows you to convert DateTime
objects into a readable string format. The format string “MMMM dd, yyyy” will display the month in full, the day with a leading zero if necessary, and the full year.
First and Last Day of the Year in C# – Complete Code
Below is a simple C# console application that demonstrates how to obtain the first and last day of the current year. This code can be compiled and run in any C# development environment, such as Visual Studio or Visual Studio Code.
using System;
namespace DateTimeExample
{
class Program
{
static void Main(string[] args)
{
// Get the current year
int currentYear = DateTime.Now.Year;
// Get the first day of the year
DateTime firstDayOfYear = new DateTime(currentYear, 1, 1);
// Get the last day of the year
DateTime lastDayOfYear = new DateTime(currentYear, 12, 31);
// Print the first day of the year
Console.WriteLine("The first day of the year is: " + firstDayOfYear.ToString("MMMM dd, yyyy"));
// Print the last day of the year
Console.WriteLine("The last day of the year is: " + lastDayOfYear.ToString("MMMM dd, yyyy"));
// Keep the console window open
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
When you run this program, it will output something like this:
The first day of the year is: January 01, 2024
The last day of the year is: December 31, 2024
Press any key to exit.
The ToString
method is used to format the dates in a human-readable form, specifically “Month Day, Year” format. The Console.ReadKey()
at the end is used to prevent the console window from closing immediately after the program runs, allowing you to see the output before exiting.
You can see the screenshot below:
Get First and Last Day of the Year in C# (Like Sun day or Mon day)
To create a complete C# program that calculates the first and last day of the current year and displays their day names (like “Sunday” or “Monday”), you’ll need to use the DateTime
class available in the .NET Framework. The program will:
- Determine the current year.
- Find the first day of the year (January 1st) and the last day of the year (December 31st).
- Display the day names for both dates.
Here, you can see the complete program that you can execute using Visual Studio.
using System;
class Program
{
static void Main()
{
// Get the current year
int currentYear = DateTime.Now.Year;
// First day of the year
DateTime firstDay = new DateTime(currentYear, 1, 1);
string firstDayName = firstDay.DayOfWeek.ToString();
// Last day of the year
DateTime lastDay = new DateTime(currentYear, 12, 31);
string lastDayName = lastDay.DayOfWeek.ToString();
// Output the results
Console.WriteLine("First Day of the Year: " + firstDayName);
Console.WriteLine("Last Day of the Year: " + lastDayName);
}
}
Once you run the code, you can see the output in the screenshot below:
Conclusion
With the DateTime
structure in C#, finding the first and last day of the current year is straightforward. In this C# tutorial, I have explained how to find the first and last day of the current year in C#.
You may also like:
- How to Find Prime Numbers in an Array in C#?
- How to Get Start and End Date of Month in C#?
- Write A Program To Demonstrate A Division By Zero Exception In C#
- How To Add Leading Zeros To String In C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…