. NET TIME Operations-DateTime class, TimeSpan class

Source: Internet
Author: User
Tags net time ole

The DateTime class is used in. NET to process time-type data.

First, the field

MaxValue represents the maximum possible value for a DateTime. This field is read-only.
MinValue represents the minimum possible value for a DateTime. This field is read-only.

Second, the attribute

Date gets the day portion of this instance.
Day Gets the date that this instance represents is the day ordinal of the month.
DayOfWeek Gets the date that this instance represents is the day of the week.
DayOfYear gets the date represented by this instance is the day ordinal of the year.
Hour gets the hour portion of the date represented by this instance.
Kind gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
Millisecond gets the millisecond portion of the date represented by this instance.
Minute gets the minute portion of the date represented by this instance.
Month gets the months portion of the date represented by this instance.
Now static, gets a DateTime object that is set to the current date and time on this computer, expressed as local time.
Second gets the seconds portion of the date represented by this instance.
Ticks gets the number of ticks that represent the date and time of this instance.
TimeOfDay gets the time of day for this instance.
Today static, gets the current date.
UtcNow Static, gets a DateTime object that is set to the current date and time on this computer, expressed as Coordinated Universal Time (UTC).
Year gets the years portion of the date represented by this instance.

        static void Main (string[] args) {Console.WriteLine (datetime.maxvalue);   Output 9999/12/31 23:59:59 Console.WriteLine (datetime.minvalue);     Output 0001/1/1 0:00:00 DateTime dt = DateTime.Now;          Gets the current time Console.WriteLine (DT);  Output 2013/3/2 12:47:12 DateTime dt1 = datetime.today;         Gets the current date Console.WriteLine (DT1); Output 2013/3/2 0:00:00 DateTime dt2 = Datetime.utcnow;         Access to international Standard Time Console.WriteLine (DT2); Output 2013/3/2 4:50:14 Console.WriteLine (dt.     Date); The output 2013/3/2 0:00:00 datetime part of the date section Console.WriteLine (dt.      Day); The first day of the output 21 months, that is today is the number of Console.WriteLine (dt.    DayOfWeek); Output Saturday weeks Console.WriteLine (dt.    DayOfYear); Output 61 today is the first day of the year Console.WriteLine (dt.     Hour); Output 12 is now a few Console.WriteLine (dt.     Kind);  The output local indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.          Console.WriteLine (Dt.millisecond); Output 720 ms section Console.WriteLine (dt.   Minute); Output 57-minute section Console.WriteLine (dt.    Month); Output March Console.WriteLine (dt.   Second); Output 27 seconds Console.WriteLine (dt.            Ticks); Output 634978260185598529 Console.WriteLine (dt.        TimeOfDay); Output 13:01:47.3999343 Console.WriteLine (dt.     year);        Output Console.readkey (); }
Third, the method

Add adds the value of the specified TimeSpan to the value of this instance.
AddDays adds the specified number of days to the value of this instance.
AddHours adds the specified number of hours to the value of this instance.
Addmilliseconds adds the specified number of milliseconds to the value of this instance.
AddMinutes adds the specified number of minutes to the value of this instance.
AddMonths adds the specified number of months to the value of this instance.
AddSeconds adds the specified number of seconds to the value of this instance.
Addticks adds the specified number of ticks to the value of this instance.
AddYears adds the specified number of years to the value of this instance.
Compare compares two instances of a DateTime and returns an integer that indicates whether the first instance is earlier than, equal to, or later than the second instance.
CompareTo is overloaded. Compares the value of this instance to a specified datetime value and indicates whether this instance is earlier than, equal to, or later than the specified datetime value.
DaysInMonth returns the number of days in the specified year and month.
Equals is overloaded. Returns a value that indicates whether two DateTime objects, or one DateTime instance and another object or datetime, are equal.
Frombinary deserializes a 64-bit binary value and re-creates the serialized DateTime initial object.
Fromfiletime converts the specified Windows file time to the equivalent local time.
FROMFILETIMEUTC converts the specified Windows file time to the equivalent UTC time.
FromOADate returns a DateTime equivalent to the specified OLE Automation date.
Getdatetimeformats is overloaded. Converts the value of this instance to all string representations supported by the standard DateTime format specifier.
GetHashCode returns the hash code for this instance. (Rewrite ValueType..::.) GetHashCode () () (). )
GetTypeCode returns the TypeCode of a value type DateTime.
IsDaylightSavingTime Indicates whether this DateTime instance is within the daylight saving time range of the current timezone.
Isleapyear returns whether the specified year is an indication of leap years.
Parse has been overloaded. Converts the specified string representation of a date and time to its datetime equivalent.
ParseExact is overloaded. Converts the specified string representation of a date and time to its datetime equivalent. The format of the string representation must exactly match the specified format, or an exception will be thrown.
Specifykind creates a new DateTime object that represents the same time as the specified datetime, but is specified as local time or Coordinated Universal Time (UTC), as indicated by the specified DateTimeKind value, or Neither.
Subtract is overloaded. Subtracts the specified time or duration from this instance.
Tobinary serializes the current DateTime object into a 64-bit binary value, which can then be used to recreate the DateTime object.
Tofiletime converts the value of the current DateTime object to the Windows file time.
TOFILETIMEUTC converts the value of the current DateTime object to the Windows file time.
ToLocalTime converts the value of the current DateTime object to local time.
Tolongdatestring converts the value of the current DateTime object to its equivalent long date string representation.
Tolongtimestring converts the value of the current DateTime object to its equivalent long-time string representation.
ToOADate converts the value of this instance to an equivalent OLE Automation date.
ToShortDateString converts the value of the current DateTime object to its equivalent short date string representation.
Toshorttimestring converts the value of the current DateTime object to its equivalent short-time string representation.
ToString is overloaded. Converts the value of the current DateTime object to its equivalent string representation.
ToUniversalTime converts the value of the current DateTime object to Coordinated Universal Time (UTC).
TryParse is overloaded. Converts the specified string representation of a date and time to its DateTime equivalent, and returns a value that indicates whether the conversion succeeded.
TryParseExact is overloaded. Converts the specified string representation of a date and time to its datetime equivalent. The format of the string representation must exactly match the specified format. The method returns a value that indicates whether the conversion succeeded.

Example:

static void Main (string[] args) {DateTime dt = DateTime.Now; 2013/3/2 13:13:27 DateTime dt1 = dt.            AddDays (10);         Console.WriteLine (DT1); 2013/3/12 13:13:27//Notice that the new date has been added for 10 days DateTime DT2 = dt.            AddHours (10);         Console.WriteLine (DT2); 2013/3/2 23:13:27//Notice that the new date has been added for 10 days DateTime DT3 = dt. AddHours (12);         Gator two hours, let it exceed Console.WriteLine (DT3);                                              2013/3/3 1:13:27//stay automatically added one day//the following 5 are similar//addmilliseconds adds the specified number of milliseconds to the value of this instance.                                              AddMinutes adds the specified number of minutes to the value of this instance.                                              AddMonths adds the specified number of months to the value of this instance.                                              AddSeconds adds the specified number of seconds to the value of this instance.             AddYears adds the specified number of years to the value of this instance. DateTime dt4 = dt.            AddDays (1);   int i = Datetime.compare (DT,DT4); The first parameter is earlier than-1 equals 0 after 1 the second parameter static version ConsoLe.   WriteLine (i);  Output-1 Int j = Dt.compareto (DT4);   The caller is 1 0 nights, 1 Console.WriteLine (j).    Output-1 int days = Datetime.daysinmonth (2011,2);    The number of days of Console.WriteLine in a certain year; Output ftime = dt.            Tofiletime ();   Console.WriteLine (Ftime); Output 130066782813286694 Windows file time datetime DT5 = Datetime.fromfiletime (130066782813286694);     Windows file time back to datetime Console.WriteLine (DT5);              Output 2013/3/2 14:11:21//The following are similar, no more write//frombinary deserializes a 64-bit binary value and re-creates the serialized DateTime initial object.              FROMFILETIMEUTC converts the specified Windows file time to the equivalent UTC time.             FromOADate returns a DateTime equivalent to the specified OLE Automation date.              Tobinary serializes the current DateTime object into a 64-bit binary value, which can then be used to recreate the DateTime object.             TOFILETIMEUTC converts the value of the current DateTime object to the Windows file time.             ToOADate converts the value of this instance to an equivalent OLE Automation date. DateTime dt6 = dt.            ToLocalTime ();  Console.WriteLine (DT6);   It was originally a local time Console.WriteLine (dt.   Tolongdatestring ()); Output March 2, 2013 Short Date string representation Console.WriteLine (dt.   Tolongtimestring ()); Output 14:52:33 short time string representation Console.WriteLine (dt.   IsDaylightSavingTime ());   False If the DateTime instance is Console.WriteLine (Datetime.isleapyear (2013)) within the daylight saving time range of the current timezone;            False whether it is a leap year string str = "2012-12-12";     DateTime dt7 = DateTime.Parse (str);     This conversion method if the conversion is unsuccessful will error Console.WriteLine (DT7);            Output 2012/12/12 0:00:00 DateTime dt8;     Datetime.tryparse (Str,out dt8);     The advantage of this method is that the conversion is unsuccessful and will not error Console.WriteLine (DT8);            Output 2012/12/12 0:00:00 string datetimestring = "September 12, 2008 15:14 56 sec.";  DateTime dt9 = DateTime.ParseExact (datetimestring, "yyyy year mm month DD day hh point mm min ss sec", NULL);     Convert to a Time object in the format specified, this method is good Console.WriteLine (DT9);            Output 2008/9/12 15:14:56 DateTime dt10; Datetime.tryparseexact (datetimestring, "yyyy mm month DD Day HH Point mm min ss sec ", Null,system.globalization.datetimestyles.none,out dt10);     Convert to a Time object in the format specified, this method is good Console.WriteLine (dt10); Output 2008/9/12 15:14:56 Console.WriteLine (dt.    ToUniversalTime ()); Output 2013/3/2 7:12:26 convert to International time//YYYYMMDD HHMMSS Console.WriteLine (dt. Getdatetimeformats (' s ') [0].    ToString ()); Output 2013-03-02t15:25:26 Console.WriteLine (dt. Getdatetimeformats (' t ') [0].    ToString ()); Output 15:18 Console.WriteLine (dt. Getdatetimeformats (' y ') [0].    ToString ()); Output March 2013 Console.WriteLine (dt. Getdatetimeformats (' D ') [0].    ToString ()); Output March 2, 2013 Console.WriteLine (dt. Getdatetimeformats (' D ') [1].    ToString ()); Output March 2, 2013, Saturday Console.WriteLine (dt. Getdatetimeformats (' D ') [2].    ToString ()); Output Saturday, March 2, 2013 Console.WriteLine (dt. Getdatetimeformats (' M ') [0].    ToString ()); Output March 2 Console.WriteLine (dt. Getdatetimeformats (' f ') [0].    ToString ()); Output 2013 3Monthly 2nd 15:36 Console.WriteLine (dt. Getdatetimeformats (' g ') [0].    ToString ()); Output 2013/3/2 15:36 Console.WriteLine (dt. Getdatetimeformats (' R ') [0].    ToString ()); Output Sat, 15:28:10 GTM Console.WriteLine (dt.   ToString ("Yyyy*mm*dd")); The output 2013*03*02 ToString () method was overridden by typecode C = dt.            GetTypeCode ();    Console.WriteLine (C.tostring ());    Output datetime datetime dt11 = datetime.specifykind (dt, datetimekind.local);            Creates a new time based on a datetime, enumerates the specified type local time or Coordinated Universal Time (UTC), or neither.    Console.WriteLine (DT11); Output 2013/3/2 15:32:33//datetime dt12 = dt.   Addticks (1000000000 * 60);            100 nanoseconds//console.writeline (dt12);            DateTime dt13 = DateTime.Now.AddDays (10); TimeSpan ts = dt13.            Subtract (DT);   Console.WriteLine (TS);            Output 10.00:00:00 ...   TimeSpan ts1 = new TimeSpan (1,0,0,0); Create a TimeSpan object, 1 days 0:0 0 seconds DateTime dt14 = dt.            ADD (TS1); ConSole.    WriteLine (dt14); Output 2013/3/3 15:32:33 Notice that the date is more than one day DateTime dt15 = dt.            Subtract (TS1);    Console.WriteLine (dt15);        Output 2013/3/1 15:32:33 Notice that the date is less than one day Console.readkey (); }
Iv. example, calculation of two time difference

The following is an example of how the time difference is calculated based on two datetime:

DateTime dt = DateTime.Now (); TimeSpan ts = new TimeSpan (dt.  Ticks);  TimeSpan tslast = new TimeSpan (dtlast.ticks); TimeSpan tsdisparity = ts. Subtract (Tslast). Duration ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.