C # time

Source: Internet
Author: User

DateTime.Now.ToShortTimeString () DateTime dt = DateTime.Now; Dt. ToString ();//2005-11-5 13:21:25 dt. Tofiletime (). ToString ();//127756416859912816 dt. TOFILETIMEUTC (). ToString ();//127756704859912816 dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25 dt. Tolongdatestring (). ToString ();//November 5, 2005 dt. Tolongtimestring (). ToString ();//13:21:25 dt. ToOADate (). ToString ();//38661.5565508218 dt. ToShortDateString (). ToString ();//2005-11-5 dt. Toshorttimestring (). ToString ();//13:21 dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25 dt. Year.tostring ();//2005 dt. Date.tostring ();//2005-11-5 0:00:00 dt. Dayofweek.tostring ();//saturday dt. Dayofyear.tostring ();//309 dt. Hour.tostring ();//13 dt.Millisecond.ToString ();//441 dt. Minute.tostring ();//30 dt. Month.tostring ();//11 dt. Second.tostring ();//28 dt. Ticks.tostring ();//632667942284412864 dt. Timeofday.tostring ();//13:30:28.4412864 dt. ToString ();//2005-11-5 13:47:04 dt. AddYears (1). ToString ();//2006-11-5 13:47:04 dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04 dt. AdDhours (1.1). ToString ();//2005-11-5 14:53:04 dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04 dt. AddMonths (1). ToString ();//2005-12-5 13:47:04 dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05 dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10 dt. Addticks (1000). ToString ();//2005-11-5 13:47:04 dt.compareto (DT). ToString ();//0 dt. ADD (?). ToString ();//question mark is a time period dt. Equals ("2005-11-6 16:11:04"). ToString ();//false dt. Equals (DT). ToString ();//true dt. GetHashCode (). ToString ();//1474088234 dt. GetType (). ToString ();//system.datetime dt. GetTypeCode (). ToString ();//datetimedt.getdatetimeformats (' s ') [0]. ToString ();//2005-11-05t14:06:25 dt. Getdatetimeformats (' t ') [0]. ToString ();//14:06 dt. Getdatetimeformats (' y ') [0]. ToString ();//November 2005 dt. Getdatetimeformats (' D ') [0]. ToString ();//November 5, 2005 dt. Getdatetimeformats (' D ') [1]. ToString ();//2005. Dt. Getdatetimeformats (' D ') [2]. ToString ();//week 62,005 to DT. Getdatetimeformats (' D ') [3]. ToString ();//Saturday November 5, 2005 dt. Getdatetimeformats (' M ') [0]. ToString ();//1January 5 dt. Getdatetimeformats (' f ') [0]. ToString ();//November 5, 2005 14:06 dt. Getdatetimeformats (' g ') [0]. ToString ();//2005-11-5 14:06 dt. Getdatetimeformats (' R ') [0]. ToString ();//sat, 2005 14:06:25 GMT string. Format ("{0:d}", dt);//2005-11-5 string. Format ("{0}", dt);//November 5, 2005 string. Format ("{0:f}", dt);//November 5, 2005 14:23 string. Format ("{0:f}", dt);//November 5, 2005 14:23:23 string. Format ("{0:g}", DT);//2005-11-5 14:23 string. Format ("{0:g}", DT);//2005-11-5 14:23:23 string. Format ("{0:m}", dt);//November 5 string. Format ("{0:r}", DT);//sat, 2005 14:23:23 GMT string. Format ("{0:s}", dt);//2005-11-05t14:23:23 string. Format ("{0:t}", dt);//14:23 string. Format ("{0:t}", dt);//14:23:23 string. Format ("{0:u}", DT);//2005-11-05 14:23:23z string. Format ("{0:u}", dt);//November 5, 2005 6:23:23 string. Format ("{0:y}", dt);//November 2005 string. Format ("{0}", dt);//2005-11-5 14:23:23 string. Format ("{0:yyyymmddhhmmssffff}", DT); Calculates the difference in the number of days between 2 dates-----------------------------------------------DateTime dt1 = Convert.datetime ("2007-8-1"); DatetIME DT2 = convert.datetime ("2007-8-15"); TimeSpan span = dt2. Subtract (DT1); int Daydiff = span. Days + 1; Calculates the number of days in a month-----------------------------------------------int day = Datetime.daysinmonth (2007, 8); days = 31; Add one day to date, reduce one day-----------------------------------------------DateTime DT =datetime.now; Dt. AddDays (1); Increase the day dt. AddDays (-1);//decrease one day other years method similar ... Oracle SQL convert Date function-----------------------------------------------to_date ("2007-6-6", "yyyy-mm-dd"); To_date ("2007/6/6", "yyyy/mm/dd"); such as the next set of data, how to find the table contains September records: Cggc_stratdate cggc_enddate ========================================= 2007-8-4 2007-9-5 2007-9-5 2007-9-20 2007-9-22 2007-10-5 SELECT * from TABLE (to_date (' 2007/9/1 ', ' yyyy/mm/dd ') between Cggc_stratdate and CG Gc_enddate OR cggc_stratdate >=to_date (' 2007/9/1 ', ' yyyy/mm/dd ') and Cggc_enddate<=to_date (' 2007/9/30 ', ' yyyy/ Mm/dd ') "OR to_date (' 2007/9/30 ', ' yyyy/mm/dd ') between Cggc_stratdate and Cggc_enddate) ORDER by cggc_stratdate ASC//this year, Formatting with ToString characters it's easy to figure out the first and last day of the year. DAtetime.parse (DateTime.Now.ToString ("yyyy-01-01")). ToShortDateString ();D Atetime.parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (1). AddDays (-1). ToShortDateString ();//Last year, no more explaining DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (-1). ToShortDateString ();D Atetime.parse (DateTime.Now.ToString ("yyyy-01-01")). AddDays (-1). ToShortDateString ();//Next year DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (1). ToShortDateString ();D Atetime.parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (2). AddDays (-1). ToShortDateString ();//This quarter, many people will feel the difficulty here, need to write a long process to judge. In fact, we all know that four quarters a year, a quarter of three months//first we push the date to the first month of the quarter, and then the first day of the month is the first day of the quarter. DateTime.Now.AddMonths (0-(DateTime.Now.Month- 1) (% 3)). ToString ("yyyy-mm-01");//Similarly, the last day of the quarter is the first day of the next quarter minus one DateTime.Parse (DateTime.Now.AddMonths (3-(DATETIME.NOW.MONTH-1) % 3)). ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ()///Next quarter DateTime.Now.AddMonths (3-(((datetime.now.month-1)% 3)). ToString ("yyyy-mm-01");D Atetime.parse (DateTime.Now.AddMonths (6-(DateTIME.NOW.MONTH-1) (% 3)). ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();//Last quarter DateTime.Now.AddMonths ( -3-(((datetime.now.month-1)% 3)). ToString ("yyyy-mm-01");D atetime.parse (DateTime.Now.AddMonths (0-((datetime.now.month-1)% 3)). ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();//How to get the number of days of the month int m=system.datetime.daysinmonth (System.DateTime.Now.Year, System.DateTime.Now.Month)///Get the actual working day of the month (that is, excluding week 6th)//Call//int days =getdays (System.DateTime.Now));p rivate int GetDays ( System.DateTime date1) {int M=system.datetime.daysinmonth (date1. Year,date1.    Month);    int mm=0; for (int i=1;i<=m;i++) {System.DateTime date=convert.todatetime (date1. year+ "-" +date1.        month+ "-" +i); Switch (date. DayOfWeek) {case System.DayOfWeek.Monday:case System.DayOfWeek.Thursday:case Sy Stem. DayOfWeek.Tuesday:case System.DayOfWeek.Wednesday:case system.dayofweek.friday:mm=                mm+1;     Break   }} return mm;} Obtain a valid working day between any two dates (excluding week 6th) to obtain a valid working day between any two dates (excluding week 6th) #region obtain a valid working day between any two dates (excluding weeks 6th)//Call//datetime Date1=convert.todate        Time ("2005-10-20");        DateTime date2=convert.todatetime ("2005-11-01");        int days =getdays (DATE1,DATE2); private int getDays (System.DateTime date1,system.datetime date2) {string M=datediff (enumdatecompare.day , Date1,date2).                       ToString ("F0");            int mm=0; for (int i=0;i<=convert.toint32 (m); i++) {System.DateTime date=convert.todatetime (date1.                AddDays (i)); Switch (date. DayOfWeek) {case System.DayOfWeek.Monday:case System.DayOfWeek.Thur                    Sday:case System.DayOfWeek.Tuesday:case System.DayOfWeek.Wednesday:                        Case system.dayofweek.friday:mm=mm+1;                Break     }                      } return mm; } #endregion格式输出格式输出 #region format output private void Page_Load (object sender, System.EventArgs e) {System.Globalization.Date Timeformatinfo Mydtfi = new System.Globalization.CultureInfo ("en-us", false). datetimeformat;//China with Zh-cndatetime myDT =system.datetime.now; Response.Write (Mydt.tostring ("F", Mydtfi))/**//**//**//*this code produces the following output. FORMAT en-US examplechar VALUE of associated property, IF anyd 1/3/2002 m/d/yyyy (Shortdatepatt Ern) D Thursday, January, 2002 dddd, MMMM DD, yyyy (Longdatepattern) F Thursday, January 03, 2 002 AMF Thursday, January, 2002 12:00:00 AM dddd, MMMM dd, yyyy h:mm:ss TT (FULLDATETIMEPA Ttern) G 1/3/2002, AMG 1/3/2002 12:00:00 AMm January, MMMM DD (monthdaypattern) M J             Anuary MMMM DD (monthdaypattern) R Thu, Jan 2002 00:00:00 GMT       DDD, dd MMM yyyy HH ': ' mm ': ' SS ' GMT ' (rfc1123pattern) R Thu, Jan 2002 00:00:00 GMT DDD, DD MMM yyyy HH ': ' mm ': ' SS ' GMT ' (rfc1123pattern) s 2002-01-03t00:00:00 yyyy '-' mm '-' dd ' T ' HH ': ' mm ': ' SS (so Rtabledatetimepattern) T-h:mm tt (Shorttimepattern) t 12:00:00 am h:m M:SS tt (Longtimepattern) u 2002-01-03 00:00:00z yyyy '-' mm '-' dd HH ': ' mm ': ' SS ' Z ' (Universalsortableda Tetimepattern) U Thursday, January, 2002 8:00:00 AMy January, 2002 MMMM, YYYY (yearmonthpatte RN) Y January, 2002 MMMM, YYYY (yearmonthpattern) */} #endregion获得本周的周六和周日ConvertDateToWeek #region Con Vertdatetoweekpublic static void Convertdatetoweek (DateTime date,out datetime firstdate,out datetime lastdate) { DateTime first=system.datetime.now;datetime last=system.datetime.now;switch (date. DayOfWeek) {case System.dayofweek.monday:first=date. AddDays ( -1); last=Date. AddDays (5); Break;case system.dayofweek.tuesday:first=date. AddDays ( -2); last=date. AddDays (4); Break;case system.dayofweek.wednesday:first=date. AddDays ( -3); last=date. AddDays (3); Break;case system.dayofweek.thursday:first=date. AddDays ( -4); last=date. AddDays (2); Break;case system.dayofweek.friday:first=date. AddDays ( -5); last=date. AddDays (1); Break;case system.dayofweek.saturday:first=date. AddDays ( -6); Last=date;break;case system.dayofweek.sunday:first=date;last=date. AddDays (6); break;} Firstdate=first;lastdate=last;} #endregion//Call DateTime firstdate=system.datetime.now;datetime Lastdate=system.datetime.now; Convertdatetoweek (date,out firstdate,out lastdate); Gets the current date is the first week of the year datetime dt = Convert.todatetime ("2006-05-01"); int weeks = dt. DAYOFYEAR/7 + 1;

  

C # time

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.