Specify a time duration in days, hours, minutes, seconds. - CSharp Language Basics

CSharp examples for Language Basics:Date Time

Description

Specify a time duration in days, hours, minutes, seconds.

Demo Code

using System;//from w ww .ja  v a2s. c  o  m
class Program
{
   static void Main(string[] args)
   {
      // Specify a time duration in days, hours, minutes, seconds.
      TimeSpan duration = new TimeSpan(3, 0, 0, 0);
      DateTime thisMoment = DateTime.Today;
       
      DateTime threeDaysFromNow = thisMoment.Add(duration);
      Console.WriteLine("Three days from now is {0}", threeDaysFromNow);
      TimeSpan duration1 = new TimeSpan(1, 0, 0);  // One hour later.
      DateTime anHourAfterMidnight = DateTime.Today.Add(duration1);
      Console.WriteLine("An hour from midnight will be {0}", anHourAfterMidnight);
      DateTime midnight = anHourAfterMidnight.Subtract(duration1);
      Console.WriteLine("An hour before 1 AM is {0}", midnight);
   }
}

Result


Related Tutorials