Calculate X Days From A Date With C# .NET

C# is a multi-paradigm language and has properties of various programming languages such as C++, Java and Basic. C# is based on the Common Language Infrastructure (CLI) specification.

A large number of languages are available today using this framework. C# is one of them is mainly preferred by enterprises, corporations and small businesses which need object-oriented programming to develop complex software applications.

Days From X

My favourite class in C# is the Date that allows us to calculate dates and offer many options.

The following C# program prompts the user to provide a date and days from the date to calculate (including a minus number to calculate the date in the past).

Once the date is calculated, the program will display the entered date and the date in the past or the future.

How To Restart A Cisco Router

Note: for a US date formating, change line 10 to en-US.

To run the program, copy it to a new project and run.

using System;
using System.Globalization;

namespace days
    class Program
        static void Main(string[] args)
            CultureInfo culture = new CultureInfo("en-AU"); // Set culture info
            int days;
            Console.WriteLine("Enter a date: ");
            string getdate = Console.ReadLine();
            Console.WriteLine("Enter a number of days to calculate from the date: ");
            string input = Console.ReadLine();
            days = int.Parse(input) ;
            days = Convert.ToInt32(input);
            DateTime userDateTime;
                if (DateTime.TryParse(getdate, out userDateTime))
                    Console.WriteLine(userDateTime.ToString("d", culture));
                    DateTime answer = userDateTime.AddDays(days);
                    Console.WriteLine("Today " + userDateTime.Date.ToString("dd/MM/yyyy"));
                    Console.WriteLine( days + " days from the above date is: " + answer.ToString("dd/MM/yyyy") + " " + answer.DayOfWeek );
                    Console.WriteLine("You have entered an incorrect value.");





The result is shown below. In the example, I have entered a date and used the -5 as the number of days to calculate.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.