The following code example shows A Program to Perform Operations on Date in C#.
In the following program a menu is displayed. Further, it allows the user to enter a date, displays the data, increments the date, and compare two dates.
using System;
namespace DateClassDemo
{
class Program
{
static void Main(string[] args)
{
MyDate ob1 = new MyDate();
Console.WriteLine("Input a date... ");
ob1.getDate();
Console.WriteLine("Displaying the date...");
ob1.showDate();
Console.WriteLine("Displaying the incremented date...");
ob1.IncrementDate();
Console.WriteLine("Comparing two dates...");
ob1.CompareDates();
}
}
class MyDate
{
int date, month, year;
DateTime d;
public MyDate()
{
date = month = year = 1;
}
public MyDate(int date, int month, int year)
{
this.date = date;
this.month = month;
this.year = year;
}
public void getDate()
{
Console.WriteLine("Enter Date: ");
date = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter Month: ");
month = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter Year: ");
year = Int32.Parse(Console.ReadLine());
try
{
d = new DateTime(year, month, date);
}
catch(Exception e)
{
Console.WriteLine("Wrong Date Entered!");
Console.WriteLine(e.Message);
}
}
public void showDate()
{
Console.WriteLine("Date Entered: " + d.ToString("F"));
}
public void IncrementDate()
{
d=d.AddDays(1);
Console.WriteLine("Next Date: " + d.ToString("F"));
}
public void CompareDates()
{
DateTime d1, d2;
d1 = new DateTime();
d2 = new DateTime();
int day, m, y;
Console.WriteLine("Enter date 1: ");
Console.WriteLine("Enter Date: ");
day = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter Month: ");
m = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter Year: ");
y = Int32.Parse(Console.ReadLine());
try
{
d1 = new DateTime(y, m, day);
}
catch (Exception e)
{
Console.WriteLine("Wrong Date Entered!");
Console.WriteLine(e.Message);
}
Console.WriteLine("Enter date 2: ");
Console.WriteLine("Enter Date: ");
day = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter Month: ");
m = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter Year: ");
y = Int32.Parse(Console.ReadLine());
try
{
d2 = new DateTime(y, m, day);
}
catch (Exception e)
{
Console.WriteLine("Wrong Date Entered!");
Console.WriteLine(e.Message);
}
int c=DateTime.Compare(d1, d2);
if (c < 0)
Console.WriteLine("Date 1 is smaller than Date 2!");
else if(c==0)
Console.WriteLine("Both Dates are equal!");
else
Console.WriteLine("Date 1 is greater than Date 2!");
}
}
}
Output

Further Reading
How to Create Instance Variables and Class Variables in Python
Comparing Rows of Two Tables with ADO.NET
Example of Label and Textbox Control in ASP.NET
One Dimensional and Two Dimensuonal Indexers in C#
Private and Static Constructors in C#
- Angular
- ASP.NET
- Bootstrap
- C
- C#
- C++
- CSS
- Deep Learning
- Django
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- MEAN Stack
- MERN Stack
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Scratch 3.0
- SQL
- TypeScript
- VB.NET
- Web Designing
- XML
