首页 > 代码库 > C#时间

C#时间

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();//2005年11月5日 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();//问号为一个时间段 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();//2005年11月 dt.GetDateTimeFormats(‘D‘)[0].ToString();//2005年11月5日 dt.GetDateTimeFormats(‘D‘)[1].ToString();//2005 11 05 dt.GetDateTimeFormats(‘D‘)[2].ToString();//星期六 2005 11 05 dt.GetDateTimeFormats(‘D‘)[3].ToString();//星期六 2005年11月5日 dt.GetDateTimeFormats(‘M‘)[0].ToString();//11月5日 dt.GetDateTimeFormats(‘f‘)[0].ToString();//2005年11月5日 14:06 dt.GetDateTimeFormats(‘g‘)[0].ToString();//2005-11-5 14:06 dt.GetDateTimeFormats(‘r‘)[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT string.Format("{0:d}",dt);//2005-11-5 string.Format("{0}",dt);//2005年11月5日 string.Format("{0:f}",dt);//2005年11月5日 14:23 string.Format("{0:F}",dt);//2005年11月5日 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);//11月5日 string.Format("{0:R}",dt);//Sat, 05 Nov 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);//2005年11月5日 6:23:23 string.Format("{0:Y}",dt);//2005年11月 string.Format("{0}",dt);//2005-11-5 14:23:23 string.Format("{0:yyyyMMddHHmmssffff}",dt); 计算2个日期之间的天数差 ----------------------------------------------- DateTime dt1 = Convert.DateTime("2007-8-1"); DateTime dt2 = Convert.DateTime("2007-8-15"); TimeSpan span = dt2.Subtract(dt1); int dayDiff = span.Days + 1; 计算某年某月的天数 ----------------------------------------------- int days = DateTime.DaysInMonth(2007, 8); days = 31; 给日期增加一天、减少一天 ----------------------------------------------- DateTime dt =DateTime.Now; dt.AddDays(1); //增加一天 dt.AddDays(-1);//减少一天 其它年份方法类似... Oracle SQL里转换日期函数 ----------------------------------------------- to_date("2007-6-6",‘YYYY-MM-DD"); to_date("2007/6/6",‘yyyy/mm/dd"); 如下一组数据,如何查找表里包含9月份的记录: 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 CGGC_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 //本年度,用ToString的字符格式化我们也很容易地算出本年度的第一天和最后一天DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();//上年度,不用再解释了吧DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString();DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();//下年度DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString();DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();//本季度,很多人都会觉得这里难点,需要写个长长的过程来判断。其实不用的,我们都知道一年四个季度,一个季度三个月//首先我们先把日期推到本季度第一个月,然后这个月的第一天就是本季度的第一天了DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");//同理,本季度的最后一天就是下季度的第一天减一DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//下季度DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//上季度DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//如何获得当月有多少天int m=System.DateTime.DaysInMonth(System.DateTime.Now.Year,System.DateTime.Now.Month);//获取某月的实际工作日(即不包括周六日)//调用//int days =getDays(System.DateTime.Now));private 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 System.DayOfWeek.Tuesday:            case System.DayOfWeek.Wednesday:            case System.DayOfWeek.Friday:                mm=mm+1;                break;        }                   }    return mm;}获得任意两日期之间的有效工作日(不包括周六日) 获得任意两日期之间的有效工作日(不包括周六日)#region 获得任意两日期之间的有效工作日(不包括周六日)//调用        //DateTime date1=Convert.ToDateTime("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.Thursday:                    case System.DayOfWeek.Tuesday:                    case System.DayOfWeek.Wednesday:                    case System.DayOfWeek.Friday:                        mm=mm+1;                        break;                }                           }            return mm;        }        #endregion格式输出格式输出#region 格式输出private void Page_Load(object sender, System.EventArgs e){System.Globalization.DateTimeFormatInfo myDTFI = new System.Globalization.CultureInfo( "en-US", false ).DateTimeFormat;//中国用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 (ShortDatePattern)D     Thursday, January 03, 2002                    dddd, MMMM dd, yyyy (LongDatePattern)f     Thursday, January 03, 2002 12:00 AMF     Thursday, January 03, 2002 12:00:00 AM                    dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)g     1/3/2002 12:00 AMG     1/3/2002 12:00:00 AMm     January 03                   MMMM dd (MonthDayPattern)M     January 03                    MMMM dd (MonthDayPattern)r     Thu, 03 Jan 2002 00:00:00 GMT                    ddd, dd MMM yyyy HH‘:‘mm‘:‘ss ‘GMT‘ (RFC1123Pattern)R     Thu, 03 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 (SortableDateTimePattern)t     12:00 AM                    h:mm tt (ShortTimePattern)T     12:00:00 AM                    h:mm:ss tt (LongTimePattern)u     2002-01-03 00:00:00Z                    yyyy‘-‘MM‘-‘dd HH‘:‘mm‘:‘ss‘Z‘ (UniversalSortableDateTimePattern)U     Thursday, January 03, 2002 8:00:00 AMy     January, 2002                    MMMM, yyyy (YearMonthPattern)Y     January, 2002                    MMMM, yyyy (YearMonthPattern)*/}#endregion获得本周的周六和周日ConvertDateToWeek#region ConvertDateToWeekpublic 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//调用DateTime firstdate=System.DateTime.Now;DateTime lastdate=System.DateTime.Now;ConvertDateToWeek(date,out firstdate,out lastdate);获得当前日期是该年度的第几周DateTime dt = Convert.ToDateTime("2006-05-01");int weeks = dt.DayOfYear / 7 + 1;

  

C#时间