Various uses of datetime in C #

Source: Internet
Author: User

Get current system time: DateTime dt = DateTime.Now; Environment.tickcount can get the "system boot to Present" millisecond value DateTime now = DateTime.Now; Console.WriteLine (now.  ToString ("Yyyy-mm-dd")); Output sconsole.writeline in YYYY-MM-DD format (dt.    ToString ()); 26/11/2009 AM 11:21:30console.writeline (dt. Tofiletime ().   ToString ()); 129036792908014024//converts the value of the current System.DateTime object to a Windows file Timeconsole.writeline (dt. TOFILETIMEUTC ().  ToString ()); 129036792908014024//converts the value of the current System.DateTime object to a Windows file Timeconsole.writeli NE (dt. ToLocalTime ().   ToString ()); 26/11/2009 AM 11:21:30//Converts the value of the current System.DateTime object to local time. Console.WriteLine (dt. Tolongdatestring ().   ToString ()); November 26, 2009 Console.WriteLine (dt. Tolongtimestring ().  ToString ()); AM 11:21:30console.writeline (dt. ToOADate ().   ToString ()); 40143.4732731597console.writeline (dt. ToShortDateString ().   ToString ()); 26/11/2009console.wrIteline (dt. Toshorttimestring ().   ToString ()); AM 11:21console.writeline (dt. ToUniversalTime ().   ToString ()); 26/11/2009 AM 3:21:30console.writeline (dt.   Year.tostring ()); 2009console.writeline (dt.   Date.tostring ()); 26/11/2009 AM 12:00:00console.writeline (dt.  Dayofweek.tostring ()); Thursdayconsole.writeline (dt.   Dayofyear.tostring ()); 330console.writeline (dt.       Hour.tostring ());   11console.writeline (Dt.Millisecond.ToString ()); 801 (ms) Console.WriteLine (dt.   Minute.tostring ()); 21console.writeline (dt.   Month.tostring ()); 11console.writeline (dt.   Second.tostring ()); 30console.writeline (dt.   Ticks.tostring ()); 633948312908014024 Console.WriteLine (dt.   Timeofday.tostring ());  12:29:51.5181524//Gets The time of day for this instance.//returns A System.TimeSpan that represents the fraction of The day is has elapsed since midnight. Console.WriteLine (dt.     ToString ());  //   26/11/2009 PM 12:29:51console.writeline (dt. AddYears (1).    ToString ()); 26/11/2010 PM 12:29:51console.writeline (dt. AddDays (1.1).    ToString ()); 27/11/2009 PM 2:53:51console.writeline (dt. AddHours (1.1).    ToString ()); 26/11/2009 PM 1:35:51console.writeline (dt. Addmilliseconds (1.1).    ToString ()); 26/11/2009 PM 12:29:51console.writeline (dt. AddMonths (1).    ToString ()); 26/12/2009 PM 12:29:51console.writeline (dt. AddSeconds (1.1).    ToString ()); 26/11/2009 PM 12:29:52console.writeline (dt. AddMinutes (1.1).    ToString ()); 26/11/2009 PM 12:30:57console.writeline (dt. Addticks (1000).    ToString ()); 26/11/2009 PM 12:29:51console.writeline (dt.compareto (DT).    ToString ()); 0console.writeline (dt. ADD (New TimeSpan (1,0,0,0)).    ToString ()); Add a time period (note: System.TimeSpan is a time period, the constructor is the following public TimeSpan (long ticks);                           Ticks:a time period expressed in 100-nanosecond units. Nanosecond: One-zero seconds new TimeSpan (10,000, 000) for one second public TimeSpan (int hours, int minutes, int. seconds);p ublic TimeSpan (int days, int hours, int minutes, int seconds);p ublic TimeSpan (int days, int hours, int minutes, int seconds, int milliseconds);) Console.WriteLine (dt. Equals ("2005-11-6 16:11:04").     ToString ()); Falseconsole.writeline (dt. Equals (DT).    ToString ()); Trueconsole.writeline (dt. GetHashCode ().    ToString ()); 1103291775console.writeline (dt. GetType ().    ToString ()); System.DateTimeConsole.WriteLine (dt. GetTypeCode ().    ToString ());   DateTime long Start = Environment.tickcount; The unit is a millisecond long End = Environment.tickcount; Console.WriteLine ("Start is:" +start); Console.WriteLine ("End is:" +end); Console.WriteLine ("The time is {0}", End-start); Console.WriteLine (dt. Getdatetimeformats (' s ') [0].    ToString ()); 2009-11-26t13:29:06console.writeline (dt. Getdatetimeformats (' t ') [0].    ToString ()); PM 1:29console.writeline (dt. Getdatetimeformats (' y ') [0].    ToString ()); November 2009 COnsole. WriteLine (dt. Getdatetimeformats (' D ') [0].    ToString ()); November 26, 2009 Console.WriteLine (dt. Getdatetimeformats (' D ') [1].    ToString ()); Thursday, 261 months, 2009console.writeline (dt. Getdatetimeformats (' D ') [2].    ToString ()); 261 months, 2009console.writeline (dt. Getdatetimeformats (' D ') [3].    ToString ()); Week 42,009 26console.writeline (dt. Getdatetimeformats (' M ') [0].    ToString ()); 261 months Console.WriteLine (dt. Getdatetimeformats (' f ') [0].    ToString ()); November 26, 2009 PM 1:29console.writeline (dt. Getdatetimeformats (' g ') [0].    ToString ()); 26/11/2009 PM 1:29console.writeline (dt. Getdatetimeformats (' R ') [0].    ToString ()); Thu, 13:29:06 GMT (note: Commonly used DateTime format: Format description output format D thin date format mm/dd/yyyy d verbose date format dddd, MMMM DD, yyyy f full Format (l Ong date + short time) dddd, MMMM dd, yyyy hh:mm F full datetime format (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss G General format (s Hort Date + short time) mm/dd/yyyy hh:mm G General format (short date + long time) mm/dd/yyyy HH:mm:ss m,m Month Day format MMMM DD s moderate datetime format y YYY-MM-DD HH: MM:SS t Compact time format hh:mm t verbose time format HH:mm:ss) Console.WriteLine (String.    Format ("{0:d}", DT)); 28/12/2009console.writeline (String.    Format ("{0:d}", DT)); December 28, 2009 Console.WriteLine (String.    Format ("{0:f}", DT)); December 28, 2009 AM 10:29console.writeline (String.    Format ("{0:f}", DT)); December 28, 2009 AM 10:29:18console.writeline (String.    Format ("{0:g}", DT)); 28/12/2009 AM 10:29console.writeline (string.    Format ("{0:g}", DT)); 28/12/2009 AM 10:29:18console.writeline (string.    Format ("{0:m}", DT)); 282 months Console.WriteLine (string.    Format ("{0:r}", DT)); Mon, Dec 10:29:18 gmtconsole.writeline (string.    Format ("{0:s}", DT)); 2009-12-28t10:29:18console.writeline (String.    Format ("{0:t}", DT)); AM 10:29console.writeline (String.    Format ("{0:t}", DT)); AM 10:29:18console.writeline (String.    Format ("{0:u}", DT)); 2009-12-28 10:29:18zconsole.writeline (String.    Format ("{0:u}", DT)); December 28, 2009 AM 2:29:18console.writeline (String.    Format ("{0:y}", DT)); December 2009 ConSole. WriteLine (String.    Format ("{0}", DT)); 28/12/2009 AM 10:29:18console.writeline (string.    Format ("{0:yyyymmddhhmmssffff}", DT));    200912281029182047 calculates the difference in the number of days between 2 dates datetime DT1 = convert.todatetime ("2007-8-1");   DateTime DT2 = Convert.todatetime ("2007-8-15"); TimeSpan span = dt2.              Subtract (DT1); int Daydiff = span.                     Days;       Calculates the number of days of the month of the year int day = Datetime.daysinmonth (2009, 8);                                       days = 31; Add one day to date, reduce one day to datetime DT =DATETIME.NOW;DT. AddDays (1); Adding a day DT itself does not change dt. AddDays (-1);//reduce the day DT itself does not change

  

Various uses of datetime in C #

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.