Subtract DateTime from Now in CSharp

Description

The following code shows how to subtract DateTime from Now.

Example


using System;/* w  ww  . ja v  a 2 s .c o m*/
using System.IO;

class Test 
{
    public static void Main() 
    {
            DateTime dt = Directory.GetCreationTime(Environment.CurrentDirectory);

            if (DateTime.Now.Subtract(dt).TotalDays > 364) 
            {
                Console.WriteLine("This directory is over a year old.");
            } 
            else if (DateTime.Now.Subtract(dt).TotalDays > 30) 
            {
                Console.WriteLine("This directory is over a month old.");
            } 
            else if (DateTime.Now.Subtract(dt).TotalDays <= 1) 
            {
                Console.WriteLine("This directory is less than a day old.");
            } 
            else 
            {
                Console.WriteLine("This directory was created on {0}", dt);
            }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var