DateTime dt = new DateTime(2011, 3, 4); DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Today; DateTime dt3 = new DateTime(2011, 3, 4, 17, 08, 09); DateTime dt4 = new DateTime(2011, 3, 4, new System.Globalization.ChineseLunisolarCalendar());//農曆日期 DateTime dt5 = new DateTime(20000, DateTimeKind.Local);
DateTime dt = DateTime.Now; s1=dt.ToString();//2019-06-25 10:38:31 s2=dt.ToString("d");//2019-06-25 s3=dt.toToLocalTime().ToString();//2019-06-25 10:38:31 s4=dt.ToUniversalTime().ToString();//2019-06-25 2:38:31
s1 = dt.ToLongDateString();//2019年6月25日 s2 = dt.ToShortDateString();//2019-06-25 s3 = dt.ToLongTimeString();//10:41:14 s4 = dt.ToShortTimeString();//10:41
s1 = dt.Date.ToString();//日期。2019-06-25 0:00:00 s2 = dt.Year.ToString();//年,2019 s3 = dt.Month.ToString();//月、6 s4 = dt.Day.ToString();//日 25 s5 = dt.DayOfWeek.ToString();//星期.Tuesday s6=Convert.ToInt32( dt.DayOfWeek).ToString(); //2 s7 = dt.DayOfYear.ToString();// 該年中的第幾天。176 s8 = (dt.DayOfYear / 7 + 1).ToString();//該年中的第幾周。26
s1 = dt.TimeOfDay.ToString();//時間。10:50:23.4881958 s2 = dt.Hour.ToString();//時,10 s3 = dt.Minute.ToString();//分、50 s4 = dt.Second.ToString();//秒 23 s5 = dt.Millisecond.ToString();//毫秒。 488
Ticks:自0001年1月1日午夜0:0:0(即DateTime.MinValue)以來通過的以100毫微秒(即納秒)爲間隔的間隔數。函數
s1 = dt.Ticks.ToString();//一個tick表明100ns,一毫秒=10000tick,一秒等於107個ticks.636970570197681958
s1 = dt.AddYears(1).ToString();// 2020-06-25 11:01:09 s2 = dt.AddMonths(-1).ToString();// 2019-05-25 11:01:24 s3 = dt.AddDays(1).ToString();// 2019-06-26 11:01:24 s4 = dt.AddHours(-1).ToString();// 2019-06-25 10:01:24 s5 = dt.AddMinutes(1).ToString();//2019-06-25 11:02:24 s6 = dt.AddTicks(1000).ToString();//2019-06-25 11:01:24
日期加減TimeSpan:spa
DateTime dt = DateTime.Now; s1 = dt + TimeSpan.FromDays(2.1).ToString();//2019-06-25 11:05:392.02:24:00 s2 = dt.Add(TimeSpan.FromDays(2.1)).ToString();//2019-06-27 13:29:39 s3 = (dt - new TimeSpan(1, 2, 0, 0, 0)).ToString();// 2019-06-24 9:05:39 s4 = dt.Subtract(TimeSpan.FromDays(2)).ToString();//2019-06-23 11:05:39
s1 =( dt - dt.AddDays(-3)).Days.ToString();//3 s2 = (dt.Subtract(dt.AddDays(-3))).Days.ToString();//3
s1 = dt.CompareTo(dt.AddYears(1)).ToString();//-1 s2 = (dt == dt.AddYears(1)).ToString();//False s3 = (dt < dt.AddYears(1)).ToString();// True
s1 = DateTime.DaysInMonth(2019, 6).ToString();//某月共有幾天,30 s2 = DateTime.IsLeapYear(2019).ToString();//是否爲閏年,False s3 = (DateTime.Compare(dt, new DateTime(2019, 6, 25))).ToString();//比較. 1 s4 = DateTime.Parse("2019,5,16").ToString();//2019-05-16 0:00:00 s5 = DateTime.ParseExact("1899-12-30", "yyyy-MM-dd", CultureInfo.CurrentCulture.DateTimeFormat).ToString();//1899-12-30 0:00:00 s6 = Convert.ToDateTime("2019,5,16").ToString();//2019-05-16 0:00:00
一、見上面的dt.DayOfYear。code
二、利用GregorianCalendarorm
GregorianCalendar gc = new GregorianCalendar(); int weekOfYear = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday); s1 = weekOfYear.ToString();// 26