This topic has not yet been rated - Rate this topic

DateTime Structure

April 12, 2014

Represents an instant in time, typically expressed as a date and time of day.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public struct DateTime : IComparable, IComparable<DateTime>, 
	IConvertible, IEquatable<DateTime>, IFormattable

The DateTime type exposes the following members.

  NameDescription
Public methodDateTime(Int64)Initializes a new instance of the DateTime structure to a specified number of ticks.
Public methodDateTime(Int64, DateTimeKind)Initializes a new instance of the DateTime structure to a specified number of ticks and to Coordinated Universal Time (UTC) or local time.
Public methodDateTime(Int32, Int32, Int32)Initializes a new instance of the DateTime structure to the specified year, month, and day.
Public methodDateTime(Int32, Int32, Int32, Calendar)Initializes a new instance of the DateTime structure to the specified year, month, and day for the specified calendar.
Public methodDateTime(Int32, Int32, Int32, Int32, Int32, Int32)Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.
Public methodDateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and Coordinated Universal Time (UTC) or local time.
Public methodDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second for the specified calendar.
Public methodDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond.
Public methodDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time.
Public methodDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond for the specified calendar.
Public methodDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.
Top
  NameDescription
Public propertyDateGets the date component of this instance.
Public propertyDayGets the day of the month represented by this instance.
Public propertyDayOfWeekGets the day of the week represented by this instance.
Public propertyDayOfYearGets the day of the year represented by this instance.
Public propertyHourGets the hour component of the date represented by this instance.
Public propertyKindGets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
Public propertyMillisecondGets the milliseconds component of the date represented by this instance.
Public propertyMinuteGets the minute component of the date represented by this instance.
Public propertyMonthGets the month component of the date represented by this instance.
Public propertyStatic memberNowGets a DateTime object that is set to the current date and time on this computer, expressed as the local time.
Public propertySecondGets the seconds component of the date represented by this instance.
Public propertyTicksGets the number of ticks that represent the date and time of this instance.
Public propertyTimeOfDayGets the time of day for this instance.
Public propertyStatic memberTodayGets the current date.
Public propertyStatic memberUtcNowGets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).
Public propertyYearGets the year component of the date represented by this instance.
Top
  NameDescription
Public methodAddReturns a new DateTime that adds the value of the specified TimeSpan to the value of this instance.
Public methodAddDaysReturns a new DateTime that adds the specified number of days to the value of this instance.
Public methodAddHoursReturns a new DateTime that adds the specified number of hours to the value of this instance.
Public methodAddMillisecondsReturns a new DateTime that adds the specified number of milliseconds to the value of this instance.
Public methodAddMinutesReturns a new DateTime that adds the specified number of minutes to the value of this instance.
Public methodAddMonthsReturns a new DateTime that adds the specified number of months to the value of this instance.
Public methodAddSecondsReturns a new DateTime that adds the specified number of seconds to the value of this instance.
Public methodAddTicksReturns a new DateTime that adds the specified number of ticks to the value of this instance.
Public methodAddYearsReturns a new DateTime that adds the specified number of years to the value of this instance.
Public methodStatic memberCompareCompares two instances of DateTime and returns an integer that indicates whether the first DateTime instance is earlier than, the same as, or later than the second DateTime instance.
Public methodCompareTo(DateTime)Compares the value of this instance to a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.
Public methodCompareTo(Object)Compares the value of this instance to a specified object that contains a specified DateTime value, and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.
Public methodStatic memberDaysInMonthReturns the number of days in the specified month and year.
Public methodEquals(DateTime)Returns a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.
Public methodEquals(Object)Returns a value indicating whether this instance is equal to a specified object. (Overrides ValueType.Equals(Object).)
Public methodStatic memberEquals(DateTime, DateTime)Returns a value indicating whether two DateTime instances have the same date and time value.
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberFromBinaryDeserializes a 64-bit binary value and recreates an original serialized DateTime object.
Public methodStatic memberFromFileTimeConverts the specified Windows file time to an equivalent local time.
Public methodStatic memberFromFileTimeUtcConverts the specified Windows file time to an equivalent UTC time.
Public methodStatic memberFromOADateReturns a DateTime equivalent to the specified OLE Automation date.
Public methodGetDateTimeFormats()Converts the value of this instance to all the string representations supported by the standard date and time format specifiers.
Public methodGetDateTimeFormats(Char)Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier.
Public methodGetDateTimeFormats(IFormatProvider)Converts the value of this instance to all the string representations supported by the standard date and time format specifiers and the specified culture-specific formatting information.
Public methodGetDateTimeFormats(Char, IFormatProvider)Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier and culture-specific formatting information.
Public methodGetHashCodeReturns the hash code for this instance. (Overrides ValueType.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetTypeCodeReturns the TypeCode for value type DateTime.
Public methodIsDaylightSavingTimeIndicates whether this instance of DateTime is within the Daylight Saving Time range for the current time zone.
Public methodStatic memberIsLeapYearReturns an indication whether the specified year is a leap year.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberParse(String)Converts the specified string representation of a date and time to its DateTime equivalent.
Public methodStatic memberParse(String, IFormatProvider)Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information.
Public methodStatic memberParse(String, IFormatProvider, DateTimeStyles)Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information and formatting style.
Public methodStatic memberParseExact(String, String, IFormatProvider)Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
Public methodStatic memberParseExact(String, String, IFormatProvider, DateTimeStyles)Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly or an exception is thrown.
Public methodStatic memberParseExact(String, String[], IFormatProvider, DateTimeStyles)Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly or an exception is thrown.
Public methodStatic memberSpecifyKindCreates a new DateTime object that has the same number of ticks as the specified DateTime, but is designated as either local time, Coordinated Universal Time (UTC), or neither, as indicated by the specified DateTimeKind value.
Public methodSubtract(DateTime)Subtracts the specified date and time from this instance.
Public methodSubtract(TimeSpan)Subtracts the specified duration from this instance.
Public methodToBinarySerializes the current DateTime object to a 64-bit binary value that subsequently can be used to recreate the DateTime object.
Public methodToFileTimeConverts the value of the current DateTime object to a Windows file time.
Public methodToFileTimeUtcConverts the value of the current DateTime object to a Windows file time.
Public methodToLocalTimeConverts the value of the current DateTime object to local time.
Public methodToLongDateStringConverts the value of the current DateTime object to its equivalent long date string representation.
Public methodToLongTimeStringConverts the value of the current DateTime object to its equivalent long time string representation.
Public methodToOADateConverts the value of this instance to the equivalent OLE Automation date.
Public methodToShortDateStringConverts the value of the current DateTime object to its equivalent short date string representation.
Public methodToShortTimeStringConverts the value of the current DateTime object to its equivalent short time string representation.
Public methodToString()Converts the value of the current DateTime object to its equivalent string representation. (Overrides ValueType.ToString().)
Public methodToString(IFormatProvider)Converts the value of the current DateTime object to its equivalent string representation using the specified culture-specific format information.
Public methodToString(String)Converts the value of the current DateTime object to its equivalent string representation using the specified format.
Public methodToString(String, IFormatProvider)Converts the value of the current DateTime object to its equivalent string representation using the specified format and culture-specific format information.
Public methodToUniversalTimeConverts the value of the current DateTime object to Coordinated Universal Time (UTC).
Public methodStatic memberTryParse(String, DateTime)Converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.
Public methodStatic memberTryParse(String, IFormatProvider, DateTimeStyles, DateTime)Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information and formatting style, and returns a value that indicates whether the conversion succeeded.
Public methodStatic memberTryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime)Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
Public methodStatic memberTryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly. The method returns a value that indicates whether the conversion succeeded.
Top
  NameDescription
Public operatorStatic memberAdditionAdds a specified time interval to a specified date and time, yielding a new date and time.
Public operatorStatic memberEqualityDetermines whether two specified instances of DateTime are equal.
Public operatorStatic memberGreaterThanDetermines whether one specified DateTime is greater than another specified DateTime.
Public operatorStatic memberGreaterThanOrEqualDetermines whether one specified DateTime is greater than or equal to another specified DateTime.
Public operatorStatic memberInequalityDetermines whether two specified instances of DateTime are not equal.
Public operatorStatic memberLessThanDetermines whether one specified DateTime is less than another specified DateTime.
Public operatorStatic memberLessThanOrEqualDetermines whether one specified DateTime is less than or equal to another specified DateTime.
Public operatorStatic memberSubtraction(DateTime, DateTime)Subtracts a specified date and time from another specified date and time and returns a time interval.
Public operatorStatic memberSubtraction(DateTime, TimeSpan)Subtracts a specified time interval from a specified date and time and returns a new date and time.
Top
  NameDescription
Public fieldStatic memberMaxValueRepresents the largest possible value of DateTime. This field is read-only.
Public fieldStatic memberMinValueRepresents the smallest possible value of DateTime. This field is read-only.
Top
  NameDescription
Explicit interface implemetationPrivate methodIConvertible.ToBooleanInfrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToByteInfrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToCharInfrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToDateTimeInfrastructure. Returns the current DateTime object.
Explicit interface implemetationPrivate methodIConvertible.ToDecimalInfrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToDoubleInfrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToInt16Infrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToInt32Infrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToInt64Infrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToSByteInfrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToSingleInfrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToTypeInfrastructure. Converts the current DateTime object to an object of a specified type.
Explicit interface implemetationPrivate methodIConvertible.ToUInt16Infrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToUInt32Infrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetationPrivate methodIConvertible.ToUInt64Infrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Top

The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.)

Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds)

. For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.

NoteNote:

   If you are working with a ticks value that you need to convert to some other time interval, such as minutes or seconds, you should use the TimeSpan.TicksPerDay, TimeSpan.TicksPerHour, TimeSpan.TicksPerMinute, TimeSpan.TicksPerSecond, or TimeSpan.TicksPerMillisecond constant to perform the conversion. For example, to add the number of seconds represented by a specified number of ticks to the Second component of a DateTime value, you can use the expression dateValue.Second + nTicks/TimeSpan.TicksPerSecond.

The DateTime structure contains a 64-bit field composed of a private Kind field concatenated with the Ticks field. The Kind field is a 2-bit field that indicates whether the DateTime structure represents a local time, a Coordinated Universal Time (UTC), or the time in an unspecified time zone. The Kind field is used when performing time conversions between time zones, but not for time comparisons or arithmetic. The value of the Kind field can be obtained with the Kind property.

NoteNote:

An alternative to the DateTime structure for working with date and time values that have a degree of time zone awareness is the DateTimeOffset structure. The DateTimeOffset structure stores date and time information in a private DateTime field and the number of minutes by which that date and time differs from UTC in a private Int16 field. This makes it possible for a DateTimeOffset value to reflect the time in a particular time zone, whereas a DateTime value can unambiguously reflect only UTC and the local time zone's time.

Instantiating a DateTime Object

You can create a new DateTime value in any of the following ways:

  • By calling any of the overloads of the DateTime constructor that allow you to specify specific elements of the date and time value (such as the year, month, and day, or the number of ticks). The following statement illustrates a call to one of the DateTime constructors to create a date with a specific year, month, day, hour, minute, and second.

    
    DateTime date1 = new DateTime(2008, 5, 1, 8, 30, 52);
    
    
    
  • By using any compiler-specific syntax for declaring date and time values. For example, the following Visual Basic statement initializes a new DateTime value.

    
    Dim date1 As Date = #5/1/2008 8:30:52 AM#
    
    
    
  • By assigning the DateTime object a date and time value returned by a property or method. The following example assigns the current date and time, the current Coordinated Universal Time (UTC) date and time, and the current date to three new DateTime variables.

    
    DateTime date1 = DateTime.Now;
    DateTime date2 = DateTime.UtcNow;
    DateTime date3 = DateTime.Today;
    
    
    
  • By parsing the string representation of a date and time value. The Parse, ParseExact, TryParse, and TryParseExact methods all convert a string to its equivalent date and time value. The following example uses the Parse method to parse a string and convert it to a DateTime value.

    The ParseExact, TryParse, and TryParseExact methods all convert a string to its equivalent date and time value. The following example uses the Parse method to parse a string and convert it to a DateTime value.

    
    string dateString = "5/1/2008 8:30:52 AM";
    DateTime date1 = DateTime.Parse(dateString, 
                              System.Globalization.CultureInfo.InvariantCulture);
    
    
    

    Note that the TryParse and TryParseExact methods indicate whether a particular string contains a valid representation of a DateTime value at the same time as they try the conversion.

  • By calling the DateTime structure's implicit default constructor. An approximate equivalent, for compilers that support it, is declaring a DateTime value without explicitly assigning a date and time to it. The following example illustrates a call to the DateTime implicit default constructor in C# and Visual Basic, as well as a DateTime variable declaration with no assignment in Visual Basic.

    
    DateTime dat1 = new DateTime();
    // The following method call displays 1/1/0001 12:00:00 AM.
    outputBlock.Text += dat1.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\n";
    // The following method call displays True.
    outputBlock.Text += dat1.Equals(DateTime.MinValue) + "\n";
    
    
    

DateTime Values and Their String Representations

Internally, all DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight, January 1, 0001. The actual DateTime value is independent of that value's string representation or its appearance in a user interface element. The appearance of a DateTime value is the result of a formatting operation. Formatting is the process of converting a value to its string representation.

Because the appearance of date and time values is dependent on such factors as culture, international standards, application requirements, and personal preference, the DateTime structure offers a great deal of flexibility in formatting date and time values through the overloads of its ToString method. The default DateTime.ToString() method returns the string representation of a date and time value using the current culture's short date and long time pattern. The following example uses the default DateTime.ToString() method to display the date and time using the short date and long time pattern for the en-US culture, the current culture on the computer on which the example was run.


DateTime date1 = new DateTime(2008, 3, 1, 7, 0, 0);
outputBlock.Text += date1.ToString() + "\n";
// For en-US culture, displays 3/1/2008 7:00:00 AM


The DateTime.ToString(IFormatProvider) method returns the string representation of a date and time value using the short date and long time pattern of a specific culture. The following example uses the DateTime.ToString(IFormatProvider) method to display the date and time using the short date and long time pattern for the fr-FR culture.


DateTime date1 = new DateTime(2008, 3, 1, 7, 0, 0);
outputBlock.Text += date1.ToString(new System.Globalization.CultureInfo("fr-FR")) + "\n";
// Displays 01/03/2008 07:00:00


The DateTime.ToString(String) method returns the string representation of the date and time in a format defined by a standard or custom format specifier and using the formatting conventions of the current culture. The following example uses the DateTime.ToString(String) method to display the full date and time pattern for the en-US culture, the current culture on the computer on which the example was run.


DateTime date1 = new DateTime(2008, 3, 1, 7, 0, 0);
outputBlock.Text += date1.ToString("F") + "\n";
// Displays Saturday, March 01, 2008 7:00:00 AM


The DateTime.ToString(String, IFormatProvider) method returns the string representation of the date and time in a format defined by a specific format specifier and using the formatting conventions of a specific culture. The following example uses the DateTime.ToString(String, IFormatProvider) method to display the full date and time pattern for the fr-FR culture.


DateTime date1 = new DateTime(2008, 3, 1, 7, 0, 0);
outputBlock.Text += date1.ToString("F", new System.Globalization.CultureInfo("fr-FR")) + "\n";
// Displays samedi 1 mars 2008 07:00:00


DateTime Values

Descriptions of time values in the DateTime type are often expressed using the Coordinated Universal Time (UTC) standard, which is the internationally recognized name for Greenwich Mean Time (GMT). Coordinated Universal Time is the time as measured at zero degrees longitude, the UTC origin point. Daylight saving time is not applicable to UTC.

Local time is relative to a particular time zone. A time zone is associated with a time zone offset, which is the displacement of the time zone measured in hours from the UTC origin point. In addition, local time is optionally affected by daylight saving time, which adds or subtracts an hour from the length of a day. Consequently, local time is calculated by adding the time zone offset to UTC and adjusting for daylight saving time if necessary. The time zone offset at the UTC origin point is zero.

UTC time is suitable for calculations, comparisons, and storing dates and time in files. Local time is appropriate for display in user interfaces of desktop applications. Time zone-aware applications (such as many Web applications) also need to work with a number of other time zones.

If the Kind property of a DateTime object is DateTimeKind.Unspecified, it is unspecified whether the time represented is local time, UTC time, or a time in some other time zone.

DateTime Operations

A calculation using a DateTime structure, such as Add or Subtract, does not modify the value of the structure. Instead, the calculation returns a new DateTime structure whose value is the result of the calculation.

NoteNote:

Calculations and comparisons of DateTime objects are meaningful only if the objects represent times in the same time zone.

Conversion operations between time zones (such as between UTC and local time, or between time in one time zone and local time) take the local time zone's daylight saving time into account, but arithmetic and comparison operations do not.

The DateTime structure itself offers limited support for converting from one time zone to another. You can use the ToLocalTime method to convert UTC to local time, or you can use the ToUniversalTime method to convert from local time to UTC. However, a full set of time zone conversion methods is available in the TimeZoneInfo class. Using these methods, you can convert the time in any one of the world's time zones to either UTC or local time.

Each DateTime member implicitly uses the Gregorian calendar to perform its operation, with the exception of constructors that specify a calendar, and methods with a parameter derived from IFormatProvider, such as System.Globalization.DateTimeFormatInfo, that implicitly specifies a calendar.

Operations by members of the DateTime type take into account details such as leap years and the number of days in a month.

DateTime vs. TimeSpan

The DateTime and TimeSpan value types differ in that a DateTime represents an instant in time whereas a TimeSpan represents a time interval. This means, for example, that you can subtract one instance of DateTime from another to obtain a TimeSpan object that represents the time interval between them. Or you could add a positive TimeSpan to the current DateTime to obtain a DateTime value that represents a future date.

You can add or subtract a time interval from a DateTime object. Time intervals can be negative or positive, can be expressed in units such as ticks or seconds, or can be expressed as a TimeSpan object.

Implemented Interfaces

This type implements the IComparable, IComparable<T>, IEquatable<T>, IFormattable, and IConvertible interfaces. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.

The following example demonstrates how to compare roughly equivalent DateTime values, accepting a small margin of difference when declaring them equal. For information about how to compile and run this example code, see Building examples that have static TextBlock controls for Windows Phone 8.


using System;

class Example
{
   static bool RoughlyEquals(DateTime time, DateTime timeWithWindow, int windowInSeconds, int frequencyInSeconds)
   {

      long delta = (long)((TimeSpan)(timeWithWindow - time)).TotalSeconds % frequencyInSeconds;

      delta = delta > windowInSeconds ? frequencyInSeconds - delta : delta;

      return Math.Abs(delta) < windowInSeconds;
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      int window = 10;
      int freq = 60 * 60 * 2; // 2 hours;

      DateTime d1 = DateTime.Now;

      DateTime d2 = d1.AddSeconds(2 * window);
      DateTime d3 = d1.AddSeconds(-2 * window);
      DateTime d4 = d1.AddSeconds(window / 2);
      DateTime d5 = d1.AddSeconds(-window / 2);

      DateTime d6 = (d1.AddHours(2)).AddSeconds(2 * window);
      DateTime d7 = (d1.AddHours(2)).AddSeconds(-2 * window);
      DateTime d8 = (d1.AddHours(2)).AddSeconds(window / 2);
      DateTime d9 = (d1.AddHours(2)).AddSeconds(-window / 2);

      outputBlock.Text += String.Format("d1 ({0}) ~= d1 ({1}): {2}\n",
                          d1, d1, RoughlyEquals(d1, d1, window, freq));
      outputBlock.Text += String.Format("d1 ({0}) ~= d2 ({1}): {2}\n", 
                          d1, d2, RoughlyEquals(d1, d2, window, freq));
      outputBlock.Text += String.Format("d1 ({0}) ~= d3 ({1}): {2}\n", 
                          d1, d3, RoughlyEquals(d1, d3, window, freq));
      outputBlock.Text += String.Format("d1 ({0}) ~= d4 ({1}): {2}\n", 
                          d1, d4, RoughlyEquals(d1, d4, window, freq));
      outputBlock.Text += String.Format("d1 ({0}) ~= d5 ({1}): {2}\n", 
                          d1, d5, RoughlyEquals(d1, d5, window, freq));

      outputBlock.Text += String.Format("d1 ({0}) ~= d6 ({1}): {2}\n", 
                          d1, d6, RoughlyEquals(d1, d6, window, freq));
      outputBlock.Text += String.Format("d1 ({0}) ~= d7 ({1}): {2}\n", 
                          d1, d7, RoughlyEquals(d1, d7, window, freq));
      outputBlock.Text += String.Format("d1 ({0}) ~= d8 ({1}): {2}\n", 
                          d1, d8, RoughlyEquals(d1, d8, window, freq));
      outputBlock.Text += String.Format("d1 ({0}) ~= d9 ({1}): {2}\n", 
                          d1, d9, RoughlyEquals(d1, d9, window, freq));
   }
}
// The example displays output similar to the following:
//    d1 (1/28/2010 9:01:26 PM) ~= d1 (1/28/2010 9:01:26 PM): True
//    d1 (1/28/2010 9:01:26 PM) ~= d2 (1/28/2010 9:01:46 PM): False
//    d1 (1/28/2010 9:01:26 PM) ~= d3 (1/28/2010 9:01:06 PM): False
//    d1 (1/28/2010 9:01:26 PM) ~= d4 (1/28/2010 9:01:31 PM): True
//    d1 (1/28/2010 9:01:26 PM) ~= d5 (1/28/2010 9:01:21 PM): True
//    d1 (1/28/2010 9:01:26 PM) ~= d6 (1/28/2010 11:01:46 PM): False
//    d1 (1/28/2010 9:01:26 PM) ~= d7 (1/28/2010 11:01:06 PM): False
//    d1 (1/28/2010 9:01:26 PM) ~= d8 (1/28/2010 11:01:31 PM): True
//    d1 (1/28/2010 9:01:26 PM) ~= d9 (1/28/2010 11:01:21 PM): True


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

All members of this type are thread safe. Members that appear to modify instance state actually return a new instance initialized with the new value. As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.

Caution noteCaution:

Assigning an instance of this type is not thread safe on all hardware platforms because the binary representation of that instance might be too large to assign in a single atomic operation.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.