Csharp/C Sharp/Development Class/Date Time

Материал из .Net Framework эксперт
Версия от 11:43, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

Add 2 month to the date time

 
using System;
using System.Collections.Generic;
using System.Text;
class Program {
    static void Main(string[] args) {
        DateTime dt = new DateTime(2004, 10, 17);
        dt.AddMonths(2);  // Month is now December.
        Console.WriteLine("Daylight savings: {0}", dt.IsDaylightSavingTime());
    }
}


Add TimeSpan to DateTime

 
using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        DateTime dt = new DateTime();
        DateTime dt1 = new DateTime(2001, 12, 31);
        DateTime dt2 = new DateTime(2000, 12, 31, 23, 59, 59);
        TimeSpan year = new TimeSpan(365 * TimeSpan.TicksPerDay);
        for (int i = 0; i < 10; ++i)
            dt += year;
        Console.WriteLine("DT as string: {0}", dt.ToString());
    }
}


A simple clock

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// A simple clock. 
  
using System;  
  
public class SimpleClock {  
  public static void Main() {  
    string t; 
    int seconds; 
 
    DateTime dt = DateTime.Now; 
    seconds = dt.Second; 
 
    for(;;) { 
      dt = DateTime.Now; 
 
      // update time if seconds change 
      if(seconds != dt.Second) { 
        seconds = dt.Second; 
 
        t = dt.ToString("T"); 
 
        if(dt.Minute==0 && dt.Second==0)  
          t = t + "\a"; // ring bell at top of hour 
 
        Console.WriteLine(t); 
      } 
    } 
  } 
}


comparisons between DateTime objects

 
using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        // Create some date/time objects
        DateTime dt = new DateTime();
        DateTime dt1 = new DateTime(2001, 12, 31);
        DateTime dt2 = new DateTime(2000, 12, 31, 23, 59, 59);
        
        if (dt2 < dt1)
            Console.WriteLine("Dt2 < Dt1");
        else
            if (dt2 == dt1)
                Console.WriteLine("Dt2 == Dt1");
            else
                Console.WriteLine("Dt2 > Dt1");
    }
}


Constructors of DateTime

 
using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        DateTime dt = new DateTime();
        DateTime dt1 = new DateTime(2001, 12, 31);
        DateTime dt2 = new DateTime(2000, 12, 31, 23, 59, 59);
        // Print them out as strings   
        Console.WriteLine("DT as string: {0}", dt.ToString());
        Console.WriteLine("DT1 as string: {0}", dt1.ToString());
        Console.WriteLine("DT2 as string: {0}", dt2.ToString());
    }
}


Current date and time

 
using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        
        Console.WriteLine("Current Date and Time: {0}", DateTime.Now.ToString());
        Console.WriteLine("Current Date Only: {0}", DateTime.Today.ToString());
    }
}


DateTime and TimeSpan Instances

 
using System;
class MainClass {
    public static void DisplayDateTime(string name, DateTime myDateTime) {
        Console.WriteLine(name + " = " + myDateTime);
        Console.WriteLine(name + ".Year = " + myDateTime.Year);
        Console.WriteLine(name + ".Month = " + myDateTime.Month);
        Console.WriteLine(name + ".Day = " + myDateTime.Day);
        Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
        Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
        Console.WriteLine(name + ".Second = " + myDateTime.Second);
        Console.WriteLine(name + ".Millisecond = " + myDateTime.Millisecond);
        Console.WriteLine(name + ".Ticks = " + myDateTime.Ticks);
    }
    public static void Main() {
        int year = 2008;
        int month = 8;
        int day = 25;
        DateTime myDateTime = new DateTime(year, month, day);
        int hour = 23;
        int minute = 30;
        int second = 12;
        int millisecond = 5;
        DateTime myDateTime2 = new DateTime(year, month, day, hour, minute, second, millisecond);
        System.Globalization.JulianCalendar myCalendar = new System.Globalization.JulianCalendar();
        DateTime myDateTime3 = new DateTime(year, month, day, myCalendar);
        DateTime myDateTime4 = new DateTime(0);
        DisplayDateTime("myDateTime", myDateTime);
        DisplayDateTime("myDateTime2", myDateTime2);
        DisplayDateTime("myDateTime3", myDateTime3);
        DisplayDateTime("myDateTime4", myDateTime4);
        TimeSpan myTimeSpan = new TimeSpan(4, 12, 10);
        myDateTime4 += myTimeSpan;
        DisplayDateTime("myDateTime4", myDateTime4);
    }
}


DateTime Now and its calculation

 
using System;
    class MainClass
    {
        public static void Main()
        {
            TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0);
            TimeSpan timespan2 = new TimeSpan(4, 12, 0, 0);
            TimeSpan oneWeek = timespan1 + timespan2;
            DateTime now = DateTime.Now;
            DateTime past = now - oneWeek;
            DateTime future = now + oneWeek;
            Console.WriteLine("Now   : {0}", now);
            Console.WriteLine("Past  : {0}", past);
            Console.WriteLine("Future: {0}", future); 
       }
    }


Displays the words "Hello World!" on the screen, along with the current date and time

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example1_1.cs: a variation on the classic "Hello World!" program.
  This program displays the words "Hello World!" on the screen,
  along with the current date and time
*/
public class Example1_1
{
  public static void Main()
  {
    // display "Hello World!" on the screen
    System.Console.WriteLine("Hello World!");
    // display the current date and time
    System.Console.WriteLine("The current date and time is " +
      System.DateTime.Now);
  }
}


display the Date, Day, DayOfWeek, DayOfYear,Ticks, and TimeOfDayProperties of myDateTime

 
using System;
class MainClass {
    public static void Main() {
        DateTime myDateTime = DateTime.Now;
        Console.WriteLine("myDateTime.Date = " + myDateTime.Date);
        Console.WriteLine("myDateTime.Day = " + myDateTime.Day);
        Console.WriteLine("myDateTime.DayOfWeek = " + myDateTime.DayOfWeek);
        Console.WriteLine("myDateTime.DayOfYear = " + myDateTime.DayOfYear);
        Console.WriteLine("myDateTime.Ticks = " + myDateTime.Ticks);
        Console.WriteLine("myDateTime.TimeOfDay = " + myDateTime.TimeOfDay);
    }
}


Do some leap year checks

 
using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        // 
        int[] years = { 1984, 2000, 1999, 2002 };
        for (int i = 0; i < years.Length; ++i) {
            if (DateTime.IsLeapYear(years[i]))
                Console.WriteLine("Year {0} is a leap year", years[i]);
            else
                Console.WriteLine("Year {0} is NOT a leap year", years[i]);
        }
    }
}


Estimates pi by throwing points into a square. Use to compare execution times

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// CalcPi.cs -- Estimates pi by throwing points into a square. Use to
//              compare execution times.
//
//              Compile this program with the following command line:
//                  C:>csc CalcPi.cs
//
namespace CalcPi
{
    using System;
    public class CalcPi
    {
        static void Main ()
        {
            const int throws = 10000000;
            DateTime now = DateTime.Now;
            Random rand = new Random ((int) now.Millisecond);
            int Inside = 0;
            for (int i = 0; i < throws; ++i)
            {
                double cx = rand.NextDouble();
                double cy = rand.NextDouble();
                double distance = Math.Sqrt ((cx * cx) + (cy * cy));
                if (distance < 1.0)
                    ++Inside;
            }
            double pi = 4 * (double) Inside / (double) throws;
            DateTime End = DateTime.Now;
            TimeSpan Diff = End - now;
            Console.WriteLine ("pi = " + pi);
            Console.WriteLine ("Milliseconds = " + Diff.TotalMilliseconds);
        }
    }
}


Illustrates the use of DateTime and TimeSpan instances

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example9_3.cs illustrates the use of DateTime and TimeSpan instances
*/
using System;
public class Example9_3
{
  public static void DisplayDateTime(
    string name, DateTime myDateTime
  )
  {
    Console.WriteLine(name + " = " + myDateTime);
    // display the DateTime"s properties
    Console.WriteLine(name + ".Year = " + myDateTime.Year);
    Console.WriteLine(name + ".Month = " + myDateTime.Month);
    Console.WriteLine(name + ".Day = " + myDateTime.Day);
    Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
    Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
    Console.WriteLine(name + ".Second = " + myDateTime.Second);
    Console.WriteLine(name + ".Millisecond = " +
      myDateTime.Millisecond);
    Console.WriteLine(name + ".Ticks = " +
      myDateTime.Ticks);
  }

  public static void Main()
  {
    // create a DateTime instance, specifying the year,
    // month, and day
    int year = 2002;
    int month = 12;
    int day = 25;
    DateTime myDateTime = new DateTime(year, month, day);
    // create a DateTime instance, specifying the year,
    // month, day, hour, minute, second, and millisecond
    int hour = 23;
    int minute = 30;
    int second = 12;
    int millisecond = 5;
    DateTime myDateTime2 =
      new DateTime(year, month, day, hour, minute, second, millisecond);
    // create a DateTime instance, specifying the year,
    // month, day, and JulianCalendar object
    System.Globalization.JulianCalendar myCalendar =
      new System.Globalization.JulianCalendar();
    DateTime myDateTime3 =
      new DateTime(year, month, day, myCalendar);
    // create a DateTime instance, specifying the number of ticks
    DateTime myDateTime4 = new DateTime(0);
    // display the various DateTime instances
    DisplayDateTime("myDateTime", myDateTime);
    DisplayDateTime("myDateTime2", myDateTime2);
    DisplayDateTime("myDateTime3", myDateTime3);
    DisplayDateTime("myDateTime4", myDateTime4);
    // create a TimeSpan instance, and add it to myDateTime4
    TimeSpan myTimeSpan = new TimeSpan(4, 12, 10);
    myDateTime4 += myTimeSpan;
    DisplayDateTime("myDateTime4", myDateTime4);
  }
}


illustrates the use of TimeSpan properties and methods

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example9_5.cs illustrates the use of TimeSpan properties and methods
*/
using System;
public class Example9_5
{
  public static void DisplayTimeSpan(
    string name, TimeSpan myTimeSpan
  )
  {
    Console.WriteLine(name + " = " + myTimeSpan);
    // display the TimeSpan"s properties
    Console.WriteLine(name + ".Days = " + myTimeSpan.Days);
    Console.WriteLine(name + ".Hours = " + myTimeSpan.Hours);
    Console.WriteLine(name + ".Minutes = " + myTimeSpan.Minutes);
    Console.WriteLine(name + ".Seconds = " + myTimeSpan.Seconds);
    Console.WriteLine(name + ".Milliseconds = " +
      myTimeSpan.Milliseconds);
    Console.WriteLine(name + ".Ticks = " + myTimeSpan.Ticks);
  }

  public static void Main()
  {
    // create a TimeSpan instance, specifying the hours, minutes,
    // and seconds
    int hours = 4;
    int minutes = 12;
    int seconds = 10;
    TimeSpan myTimeSpan = new TimeSpan(hours, minutes, seconds);
    Console.WriteLine("myTimeSpan = " + myTimeSpan);
    // create a TimeSpan instance, specifying the days, hours, minutes,
    // and seconds
    int days = 1;
    TimeSpan myTimeSpan2 = new TimeSpan(days, hours, minutes, seconds);
    Console.WriteLine("myTimeSpan2 = " + myTimeSpan2);
    // create a TimeSpan instance, specifying the days, hours, minutes,
    // seconds, and milliseconds
    int milliseconds = 20;
    TimeSpan myTimeSpan3 =
      new TimeSpan(days, hours, minutes, seconds, milliseconds);
    Console.WriteLine("myTimeSpan3 = " + myTimeSpan3);
    // create a TimeSpan instance, specifying the number of ticks
    long ticks = 300;
    TimeSpan myTimeSpan4 = new TimeSpan(ticks);
    Console.WriteLine("myTimeSpan4 = " + myTimeSpan4);
    // display the properties for myTimeSpan
    Console.WriteLine("myTimeSpan.Days = " +
      myTimeSpan.Days);
    Console.WriteLine("myTimeSpan.Hours = " +
      myTimeSpan.Hours);
    Console.WriteLine("myTimeSpan.Minutes = " +
      myTimeSpan.Minutes);
    Console.WriteLine("myTimeSpan.Seconds = " +
      myTimeSpan.Seconds);
    Console.WriteLine("myTimeSpan.Milliseconds = " +
      myTimeSpan.Milliseconds);
    Console.WriteLine("myTimeSpan.Ticks = " +
      myTimeSpan.Ticks);
    Console.WriteLine("myTimeSpan.TotalDays = " +
      myTimeSpan.TotalDays);
    Console.WriteLine("myTimeSpan.TotalHours = " +
      myTimeSpan.TotalHours);
    Console.WriteLine("myTimeSpan.TotalMinutes = " +
      myTimeSpan.TotalMinutes);
    Console.WriteLine("myTimeSpan.TotalSeconds = " +
      myTimeSpan.TotalSeconds);
    Console.WriteLine("myTimeSpan.TotalMilliseconds = " +
      myTimeSpan.TotalMilliseconds);
    // use the FromDays(), FromHours(), FromMinutes(), FromSeconds(),
    // FromMilliseconds(), and FromTicks() methods to create new
    // TimeSpan instances
    TimeSpan myTimeSpan5 = TimeSpan.FromDays(5);
    Console.WriteLine("TimeSpan.FromDays(5) = " +
      myTimeSpan5);
    TimeSpan myTimeSpan6 = TimeSpan.FromHours(10);
    Console.WriteLine("TimeSpan.FromHours(10) = " +
      myTimeSpan6);
    TimeSpan myTimeSpan7 = TimeSpan.FromMinutes(30);
    Console.WriteLine("TimeSpan.FromMinutes(30) = " +
      myTimeSpan7);
    TimeSpan myTimeSpan8 = TimeSpan.FromSeconds(15);
    Console.WriteLine("TimeSpan.FromSeconds(15) = " +
      myTimeSpan8);
    TimeSpan myTimeSpan9 = TimeSpan.FromMilliseconds(200);
    Console.WriteLine("TimeSpan.FromMilliseconds(200) = " +
      myTimeSpan9);
    TimeSpan myTimeSpan10 = TimeSpan.FromTicks(500);
    Console.WriteLine("TimeSpan.FromTicks(500) = " +
      myTimeSpan10);
    // use the Parse() method to convert strings to TimeSpan instances
    TimeSpan myTimeSpan11 = TimeSpan.Parse("8:10:30");
    Console.WriteLine("TimeSpan.Parse(\"8:10:30\") = " +
      myTimeSpan11);
    TimeSpan myTimeSpan12 = TimeSpan.Parse("1.8:10:30.1234567");
    Console.WriteLine("TimeSpan.Parse(\"1.8:10:30.1234567\") = " +
      myTimeSpan12);
    // use the Add() method to add a TimeSpan instance to another
    TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
    TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
    TimeSpan myTimeSpan15 = myTimeSpan13.Add(myTimeSpan14);
    Console.WriteLine("myTimeSpan13 = " + myTimeSpan13);
    Console.WriteLine("myTimeSpan14 = " + myTimeSpan14);
    Console.WriteLine("myTimeSpan15 = " + myTimeSpan15);
    // use the Subtract() method to subtract a TimeSpan instance
    // from another
    myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14);
    Console.WriteLine("myTimeSpan15 = " + myTimeSpan15);
    // use the Duration() method to add two TimeSpan instances
    Console.WriteLine("myTimeSpan15.Duration() = " +
      myTimeSpan15.Duration());
    // use the Negate() method to add two TimeSpan instances
    Console.WriteLine("myTimeSpan15.Negate() = " +
      myTimeSpan15.Negate());
    Console.WriteLine("myTimeSpan14.Negate() = " +
      myTimeSpan14.Negate());
  }
}


Look at the min and max date/time values

 

using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        
        Console.WriteLine("Min Date: {0}", DateTime.MinValue.ToString());
        Console.WriteLine("Max Date: {0}", DateTime.MaxValue.ToString());
    }
}


Measures the time taken to add some numbers

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example9_6.cs measures the time taken to add some numbers
*/
using System;
public class Example9_6
{
  public static void Main()
  {
    // create a DateTime object and set it to the
    // current date and time
    DateTime start = DateTime.Now;
    // add numbers using a for loop
    long total = 0;
    for (int count = 0; count < 1000000; count++)
    {
      total += count;
    }
    // subtract the current date and time from the start,
    // storing the difference in a TimeSpan
    TimeSpan timeTaken = DateTime.Now - start;
    // display the number of milliseconds taken to add the numbers
    Console.WriteLine("Milliseconds = " + timeTaken.Milliseconds);
    // display the total of the added numbers
    Console.WriteLine("total = " + total);
  }
}


new DateTime( 29)

 

    using System;
    class MainClass
    {
        public static void Main()
        {
            DateTime dt = new DateTime(1900, 2, 29);
       }
    }


new DateTime( 29, new JulianCalendar())

 
using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        DateTime dt = new DateTime(1900, 2, 29, new JulianCalendar());
    }
}


Offset of DateTime

 
using System;
public class MainClass {
    public static void Main() {
        DateTimeOffset local = DateTimeOffset.Now;
        DateTimeOffset utc = local.ToUniversalTime();
        Console.WriteLine(local.Offset);   // -06:00:00 (in Central America)
        Console.WriteLine(utc.Offset);     // 00:00:00
        Console.WriteLine(local == utc);                 // True
        //To include the Offset in the comparison, you must use the EqualsExact method:
        Console.WriteLine(local.EqualsExact(utc));      // False
    }
}


Output DateTime object

 
using System;
using System.Globalization;
public class DatesApp {
    public static void Main(string[] args) {
        DateTime dt = DateTime.Now;
        Console.WriteLine(dt);
        Console.WriteLine("date = {0}, time = {1}\n",dt.Date, dt.TimeOfDay);
    }
}


Parse and ParseExact

 
   using System;
   
   class MainClass
   {
      public static void Main(string[] args)
      {
         DateTime dt1 = DateTime.Parse("Sep 2005");
         Console.WriteLine(dt1);
         DateTime dt2 = DateTime.Parse("Monday 5 September 2005 14:15:33");
         Console.WriteLine(dt2);
         DateTime dt3 = DateTime.Parse("5, 9,05");
         Console.WriteLine(dt3);
         DateTime dt4 = DateTime.Parse("5/9/2005 14:15:33");
         Console.WriteLine(dt4);
         DateTime dt5 = DateTime.Parse("2:15 PM");
         Console.WriteLine(dt5);
         DateTime dt6 = DateTime.ParseExact("2:13:30 PM", "h:mm:ss tt", null);
         Console.WriteLine(dt6);
         DateTime dt7 = DateTime.ParseExact("Mon, 05 Sep 2005 14:13:30 GMT","ddd, dd MMM yyyy HH":"mm":"ss "GMT"", null);
         Console.WriteLine(dt7);
         DateTime dt8 = DateTime.ParseExact("September 05", "MMMM dd", null);
         Console.WriteLine(dt8);
       }
   }


Specify Kind DateTime

 
using System;
public class MainClass {
    public static void Main() {
        DateTime d = new DateTime(2000, 12, 12);
        DateTime utc = DateTime.SpecifyKind(d, DateTimeKind.Utc);
        Console.WriteLine(utc);
    }
}


use the Add() method to add a TimeSpan to a DateTime

 
using System;
class MainClass {
    public static void Main() {
        TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
        DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30");
        DateTime myDateTime7 = myDateTime6.Add(myTimeSpan);
        Console.WriteLine("myTimeSpan = " + myTimeSpan);
        Console.WriteLine("myDateTime6.Add(myTimeSpan) = " + myDateTime7);
    }
}


use the AddYears(), AddMonths(), AddDays(), AddMinutes(), and AddSeconds() methods to add periods to a DateTime

 
using System;
class MainClass {
    public static void Main() {
        DateTime myDateTime9 = new DateTime(2004, 1, 1);
        Console.WriteLine("Initial myDateTime9 = " + myDateTime9);
        myDateTime9 = myDateTime9.AddYears(1);
        myDateTime9 = myDateTime9.AddMonths(5);
        myDateTime9 = myDateTime9.AddDays(3);
        myDateTime9 = myDateTime9.AddMinutes(30);
        myDateTime9 = myDateTime9.AddSeconds(15);
        Console.WriteLine("Final myDateTime9 = " + myDateTime9);
    }
}


use the Compare() method to compare DateTime instances

 
using System;
class MainClass {
    public static void Main() {
        DateTime myDateTime = DateTime.Now;
        DateTime myDateTime2 = DateTime.UtcNow;
        DateTime myDateTime3 = new DateTime(2004, 1, 13);
        DateTime myDateTime4 = new DateTime(2004, 1, 14);
        Console.WriteLine("myDateTime3 = " + myDateTime3);
        Console.WriteLine("myDateTime4 = " + myDateTime4);
        int intResult = DateTime.rupare(myDateTime3, myDateTime4);
        Console.WriteLine("DateTime.rupare(myDateTime3,myDateTime4) = " + DateTime.rupare(myDateTime, myDateTime2));
    }
}


use the DaysInMonth() method to retrieve the number of days in a particular month and year

 

using System;
class MainClass {
    public static void Main() {
        int days = DateTime.DaysInMonth(2004, 1);
        Console.WriteLine("DateTime.DaysInMonth(2004, 1) = " + days);
    }
}


use the Equals() method to compare DateTime instances

 
using System;
class MainClass {
    public static void Main() {
        DateTime myDateTime3 = new DateTime(2004, 1, 13);
        DateTime myDateTime4 = new DateTime(2004, 1, 14);
        bool boolResult = DateTime.Equals(myDateTime3, myDateTime4);
        Console.WriteLine("DateTime.Equals(myDateTime3,myDateTime4) = " + boolResult);
        boolResult = myDateTime3.Equals(myDateTime4);
        Console.WriteLine("myDateTime3.Equals(myDateTime4) is " + boolResult);
    }
}


use the IsLeapYear() method to determine if a particular year is a leap year

 

using System;
class MainClass {
    public static void Main() {
        bool boolResult = DateTime.IsLeapYear(2004);
        Console.WriteLine("DateTime.IsLeapYear(2004) = " + boolResult);
    }
}


use the Now and UtcNow properties to get the currrent date and time

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("DateTime.Now = " + DateTime.Now);
        Console.WriteLine("DateTime.UtcNow = " + DateTime.UtcNow);
        DateTime myDateTime = DateTime.Now;
        Console.WriteLine("myDateTime = " + myDateTime);
        DateTime myDateTime2 = DateTime.UtcNow;
        Console.WriteLine("myDateTime = " + myDateTime);
    }
}


use the overloaded addition operator (+) to add a TimeSpan to a DateTime

 
using System;
class MainClass {
    public static void Main() {
        TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
        DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30");
        DateTime myDateTime8 = myDateTime6 + myTimeSpan;
        Console.WriteLine("myDateTime6 + myTimeSpan = " + myDateTime8);
    }
}


use the overloaded less than operator (

 
using System;
class MainClass {
    public static void Main() {
        DateTime myDateTime3 = new DateTime(2004, 1, 13);
        DateTime myDateTime4 = new DateTime(2004, 1, 14);
        bool boolResult = myDateTime3 < myDateTime4;
        Console.WriteLine("myDateTime3 < myDateTime4 is " + boolResult);
    }
}


use the overloaded subtraction operator (-) to subtract a TimeSpan from a DateTime

 
using System;
class MainClass {
    public static void Main() {
        TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
        DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30");
        DateTime myDateTime8 = myDateTime6 - myTimeSpan;
        Console.WriteLine("myDateTime6 - myTimeSpan = " + myDateTime8);
    }
}


use the Parse() method to convert strings to DateTime instances

 
using System;
class MainClass {
    public static void Main() {
        DateTime myDateTime5 = DateTime.Parse("1/13/2004");
        DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30");
        Console.WriteLine("myDateTime5 = " + myDateTime5);
        Console.WriteLine("myDateTime6 = " + myDateTime6);

    }
}


use the Subtract() method to subtract a TimeSpan from a DateTime

 
using System;
class MainClass {
    public static void Main() {
        TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
        DateTime myDateTime6 = DateTime.Parse("1/13/2004 23:10:30");
        DateTime myDateTime7 = myDateTime6.Subtract(myTimeSpan);
        Console.WriteLine("myDateTime6.Subtract(myTimeSpan) = " + myDateTime7);

    }
}


What day of the month is this?

 
using System;
using System.Collections.Generic;
using System.Text;
class Program {
    static void Main(string[] args) {
        DateTime dt = new DateTime(2004, 10, 17);
        
        Console.WriteLine("The day of {0} is {1}", dt.Date, dt.DayOfWeek);
    }
}