Get Date difference - CSharp Language Basics

CSharp examples for Language Basics:Date Time

Description

Get Date difference

Demo Code

using System;//from w w  w.  j  a va2 s . c  o  m
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
   static void Main(string[] args)
   {
      // Input
      Console.Write("Enter your date of birth: ");
      DateTime dateOfBirth = DateTime.UtcNow;
      // Today
      DateTime today = DateTime.Today;
      // Date difference
      TimeSpan difference = today - dateOfBirth;
      int numberOfDays = difference.Days;
      // Output
      Console.WriteLine();
      Console.WriteLine("Today is: " + today.ToShortDateString());
      Console.WriteLine("The world likes you for this number of days: " + numberOfDays.ToString("N0"));
   }
}

Result


Related Tutorials