CSharp - Write program to Calculate Date Difference


You need to calculate the time span between two specific dates.

You need to know how many days or years have passed between the entered dates.

Read a user's date of birth.

The program displays how many days have passed.


You need to subtract the birth date from today's date.

When you subtract dates, the result is a TimeSpan object.

With TimeSpan, you can access its properties.


using System;
class Program{//from w w w .java  2  s  .co  m
    static void Main(string[] args){
        // Input 
        Console.Write("Enter your date of birth: ");
        string input = Console.ReadLine();
        DateTime dateOfBirth = Convert.ToDateTime(input);

        // Today 
        DateTime today = DateTime.Today;

        // Date difference 
        TimeSpan difference = today - dateOfBirth;
        int numberOfDays = difference.Days;

        Console.WriteLine("Today is: " + today.ToShortDateString());
        Console.WriteLine("The world likes you for this number of days: " + numberOfDays.ToString("N0"));