Various uses of datetime in C #

Source: Internet
Author: User
Tags local time

Various uses of datetime in C #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 s by YYYY-MM-DD format
Console.WriteLine (dt.    ToString ()); 26/11/2009 AM 11:21:30
Console.WriteLine (dt. Tofiletime ().   ToString ()); 129036792908014024
Converts the value of the System.DateTime object to a Windows file time
Console.WriteLine (dt. TOFILETIMEUTC ().  ToString ()); 129036792908014024
Converts the value of the System.DateTime object to a Windows file time
Console.WriteLine (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:30
Console.WriteLine (dt. ToOADate ().   ToString ()); 40143.4732731597
Console.WriteLine (dt. ToShortDateString ().   ToString ()); 26/11/2009
Console.WriteLine (dt. Toshorttimestring ().   ToString ()); AM 11:21
Console.WriteLine (dt. ToUniversalTime ().   ToString ()); 26/11/2009 AM 3:21:30
Console.WriteLine (dt.   Year.tostring ()); 2009
Console.WriteLine (dt.   Date.tostring ()); 26/11/2009 AM 12:00:00
Console.WriteLine (dt.  Dayofweek.tostring ()); Thursday
Console.WriteLine (dt.   Dayofyear.tostring ()); 330
Console.WriteLine (dt.       Hour.tostring ()); 11
Console.WriteLine (Dt.Millisecond.ToString ()); 801 (MS)
Console.WriteLine (dt.   Minute.tostring ()); 21st
Console.WriteLine (dt.   Month.tostring ()); 11
Console.WriteLine (dt.   Second.tostring ()); 30
Console.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, the has elapsed since midnight.
Console.WriteLine (dt.     ToString ()); 26/11/2009 PM 12:29:51
Console.WriteLine (dt. AddYears (1).    ToString ()); 26/11/2010 PM 12:29:51
Console.WriteLine (dt. AddDays (1.1).    ToString ()); 27/11/2009 PM 2:53:51
Console.WriteLine (dt. AddHours (1.1).    ToString ()); 26/11/2009 PM 1:35:51
Console.WriteLine (dt. Addmilliseconds (1.1).    ToString ()); 26/11/2009 PM 12:29:51
Console.WriteLine (dt. AddMonths (1).    ToString ()); 26/12/2009 PM 12:29:51
Console.WriteLine (dt. AddSeconds (1.1).    ToString ()); 26/11/2009 PM 12:29:52
Console.WriteLine (dt. AddMinutes (1.1).    ToString ()); 26/11/2009 PM 12:30:57
Console.WriteLine (dt. Addticks (1000).    ToString ()); 26/11/2009 PM 12:29:51
Console.WriteLine (Dt.compareto (DT).    ToString ()); 0
Console.WriteLine (dt. ADD (New TimeSpan (1,0,0,0)).    ToString ()); Add a time period
Note
System.TimeSpan is a time period, the constructor is as follows
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);
public TimeSpan (int days, int hours, int minutes, int seconds);
public TimeSpan (int days, int hours, int minutes, int seconds, int milliseconds);

Console.WriteLine (dt. Equals ("2005-11-6 16:11:04").     ToString ()); False
Console.WriteLine (dt. Equals (DT).    ToString ()); True
Console.WriteLine (dt. GetHashCode ().    ToString ()); 1103291775
Console.WriteLine (dt. GetType ().    ToString ()); System.DateTime
Console.WriteLine (dt. GetTypeCode ().    ToString ()); Datetime

Long Start = Environment.tickcount; Unit is milliseconds
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:06
Console.WriteLine (dt. Getdatetimeformats (' t ') [0].    ToString ()); PM 1:29
Console.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, 2009
Console.WriteLine (dt. Getdatetimeformats (' D ') [2].    ToString ()); 261 months, 2009
Console.WriteLine (dt. Getdatetimeformats (' D ') [3].    ToString ()); Thursday 2009 11 26
Console.WriteLine (dt. Getdatetimeformats (' M ') [0].    ToString ()); 261 months
Console.WriteLine (dt. Getdatetimeformats (' f ') [0].    ToString ()); November 26, 2009 PM 1:29
Console.WriteLine (dt. Getdatetimeformats (' g ') [0].    ToString ()); 26/11/2009 PM 1:29
Console.WriteLine (dt. Getdatetimeformats (' R ') [0].    ToString ()); Thu, 13:29:06 GMT
Note
Common Date-time formats:
Format Description Output format
D Thin Date format mm/dd/yyyy
D Detailed date format dddd, MMMM DD, yyyy
F Full Format (long 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 (short 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 medium Date time format Yyyy-mm-dd HH:MM:SS
T Lite time Format hh:mm
T Verbose time Format HH:mm:ss
)

Console.WriteLine (String.    Format ("{0:d}", DT)); 28/12/2009
Console.WriteLine (String.    Format ("{0:d}", DT)); December 28, 2009
Console.WriteLine (String.    Format ("{0:f}", DT)); December 28, 2009 AM 10:29
Console.WriteLine (String.    Format ("{0:f}", DT)); December 28, 2009 AM 10:29:18
Console.WriteLine (String.    Format ("{0:g}", DT)); 28/12/2009 AM 10:29
Console.WriteLine (String.    Format ("{0:g}", DT)); 28/12/2009 AM 10:29:18
Console.WriteLine (String.    Format ("{0:m}", DT)); 282 months
Console.WriteLine (String.    Format ("{0:r}", DT)); Mon, Dec 10:29:18 GMT
Console.WriteLine (String.    Format ("{0:s}", DT)); 2009-12-28t10:29:18
Console.WriteLine (String.    Format ("{0:t}", DT)); AM 10:29
Console.WriteLine (String.    Format ("{0:t}", DT)); AM 10:29:18
Console.WriteLine (String.    Format ("{0:u}", DT)); 2009-12-28 10:29:18z
Console.WriteLine (String.    Format ("{0:u}", DT)); December 28, 2009 AM 2:29:18
Console.WriteLine (String.    Format ("{0:y}", DT)); December 2009
Console.WriteLine (String.    Format ("{0}", DT)); 28/12/2009 AM 10:29:18
Console.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;

Calculate the number of days of the month in a year
int days = Datetime.daysinmonth (2009, 8);
days = 31;

Add one day to date, one day less
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 #

Related Article

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.