C# DateTime

  1 获得当前系统时间: DateTime dt = DateTime.Now;
  2 
  3 Environment.TickCount可以得到“系统启动到现在”的毫秒值
  4 
  5 DateTime now = DateTime.Now;
  6 Console.WriteLine(now.ToString("yyyy-MM-dd"));  //按yyyy-MM-dd格式输出s
  7 
  8 Console.WriteLine(dt.ToString());    //  26/11/2009 AM 11:21:30
  9 
 10 Console.WriteLine(dt.ToFileTime().ToString());   //   129036792908014024
 11 
 12 // Converts the value of the current System.DateTime object to a Windows file time 
 13 
 14 Console.WriteLine(dt.ToFileTimeUtc().ToString());  //     129036792908014024
 15 
 16 // Converts the value of the current System.DateTime object to a Windows file time
 17 
 18 Console.WriteLine(dt.ToLocalTime().ToString());   //       26/11/2009 AM 11:21:30
 19 
 20 // Converts the value of the current System.DateTime object to local time.
 21 
 22 Console.WriteLine(dt.ToLongDateString().ToString());   //      2009年11月26日
 23 
 24 Console.WriteLine(dt.ToLongTimeString().ToString());  //      AM 11:21:30
 25 
 26 Console.WriteLine(dt.ToOADate().ToString());   //      40143.4732731597
 27 
 28 Console.WriteLine(dt.ToShortDateString().ToString());   //     26/11/2009
 29 
 30 Console.WriteLine(dt.ToShortTimeString().ToString());   //     AM 11:21
 31 
 32 Console.WriteLine(dt.ToUniversalTime().ToString());   //       26/11/2009 AM 3:21:30
 33 
 34 Console.WriteLine(dt.Year.ToString());   //        2009
 35 
 36 Console.WriteLine(dt.Date.ToString());   //        26/11/2009 AM 12:00:00
 37 
 38 Console.WriteLine(dt.DayOfWeek.ToString());  //       Thursday
 39 
 40 Console.WriteLine(dt.DayOfYear.ToString());   //       330
 41 
 42 Console.WriteLine(dt.Hour.ToString());       //        11
 43 
 44 Console.WriteLine(dt.Millisecond.ToString());   //     801        (毫秒)
 45 
 46 Console.WriteLine(dt.Minute.ToString());   //      21
 47 
 48 Console.WriteLine(dt.Month.ToString());   //       11
 49 
 50 Console.WriteLine(dt.Second.ToString());   //      30
 51 
 52 Console.WriteLine(dt.Ticks.ToString());   //       633948312908014024
 53 
 54 
 55 Console.WriteLine(dt.TimeOfDay.ToString());   //       12:29:51.5181524
 56 
 57 // Gets the time of day for this instance.
 58 
 59 // 返回 A System.TimeSpan that represents the fraction of the day that has elapsed since midnight.
 60 
 61 Console.WriteLine(dt.ToString());     //     26/11/2009 PM 12:29:51
 62 
 63 Console.WriteLine(dt.AddYears(1).ToString());    //         26/11/2010 PM 12:29:51
 64 
 65 Console.WriteLine(dt.AddDays(1.1).ToString());    //        27/11/2009 PM 2:53:51
 66 
 67 Console.WriteLine(dt.AddHours(1.1).ToString());    //       26/11/2009 PM 1:35:51
 68 
 69 Console.WriteLine(dt.AddMilliseconds(1.1).ToString());    //26/11/2009 PM 12:29:51
 70 
 71 Console.WriteLine(dt.AddMonths(1).ToString());    //        26/12/2009 PM 12:29:51
 72 
 73 Console.WriteLine(dt.AddSeconds(1.1).ToString());    //     26/11/2009 PM 12:29:52
 74 
 75 Console.WriteLine(dt.AddMinutes(1.1).ToString());    //     26/11/2009 PM 12:30:57
 76 
 77 Console.WriteLine(dt.AddTicks(1000).ToString());    //      26/11/2009 PM 12:29:51
 78 
 79 Console.WriteLine(dt.CompareTo(dt).ToString());    //       0
 80 
 81 Console.WriteLine(dt.Add(new TimeSpan(1,0,0,0)).ToString());    // 加上一个时间段
 82 
 83 (注:
 84 
 85 System.TimeSpan为一个时间段,构造函数如下
 86 
 87 public TimeSpan(long ticks); // ticks: A time period expressed in 100-nanosecond units.
 88 
 89                            //nanosecond:十亿分之一秒   new TimeSpan(10,000,000)        为一秒
 90 
 91 public TimeSpan(int hours, int minutes, int seconds);
 92 
 93 public TimeSpan(int days, int hours, int minutes, int seconds);
 94 
 95 public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);
 96 
 97  98 
 99 Console.WriteLine(dt.Equals("2005-11-6 16:11:04").ToString());     //        False
100 
101 Console.WriteLine(dt.Equals(dt).ToString());    //      True
102 
103 Console.WriteLine(dt.GetHashCode().ToString());    //       1103291775
104 
105 Console.WriteLine(dt.GetType().ToString());    //       System.DateTime
106 
107 Console.WriteLine(dt.GetTypeCode().ToString());    //       DateTime
108   
109 
110 long Start = Environment.TickCount;   //单位是毫秒
111 
112 long End = Environment.TickCount;
113 
114 Console.WriteLine("Start is : "+Start);
115 
116 Console.WriteLine("End is : "+End);
117 
118 Console.WriteLine("The Time is {0}",End-Start);
119 Console.WriteLine(dt.GetDateTimeFormats('s')[0].ToString());    //2009-11-26T13:29:06
120 
121 Console.WriteLine(dt.GetDateTimeFormats('t')[0].ToString());    //PM 1:29
122 
123 Console.WriteLine(dt.GetDateTimeFormats('y')[0].ToString());    //2009年11月
124 
125 Console.WriteLine(dt.GetDateTimeFormats('D')[0].ToString());    //2009年11月26日
126 
127 Console.WriteLine(dt.GetDateTimeFormats('D')[1].ToString());    //星期四, 26 十一月, 2009
128 
129 Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString());    //26 十一月, 2009
130 
131 Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString());    //星期四 2009 11 26
132 
133 Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString());    //26 十一月
134 
135 Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString());    //2009年11月26日 PM 1:29
136 
137 Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString());    //26/11/2009 PM 1:29
138 
139 Console.WriteLine(dt.GetDateTimeFormats('r')[0].ToString());    //Thu, 26 Nov 2009 13:29:06 GMT
140 
141 (注:
142 
143 常用的日期时间格式:
144 
145 格式 说明      输出格式 
146 d 精简日期格式 MM/dd/yyyy 
147 D 详细日期格式 dddd, MMMM dd, yyyy 
148 f  完整格式    (long date + short time) dddd, MMMM dd, yyyy HH:mm 
149 F 完整日期时间格式 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss 
150 g 一般格式 (short date + short time) MM/dd/yyyy HH:mm 
151 G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss 
152 m,M 月日格式 MMMM dd 
153 s 适中日期时间格式 yyyy-MM-dd HH:mm:ss 
154 t 精简时间格式 HH:mm 
155 T 详细时间格式 HH:mm:ss
156 
157 )
158 
159 
160 Console.WriteLine(string.Format("{0:d}", dt));    //28/12/2009
161 
162 Console.WriteLine(string.Format("{0:D}", dt));    //2009年12月28日
163 
164 Console.WriteLine(string.Format("{0:f}", dt));    //2009年12月28日 AM 10:29
165 
166 Console.WriteLine(string.Format("{0:F}", dt));    //2009年12月28日 AM 10:29:18
167 
168 Console.WriteLine(string.Format("{0:g}", dt));    //28/12/2009 AM 10:29
169 
170 Console.WriteLine(string.Format("{0:G}", dt));    //28/12/2009 AM 10:29:18
171 
172 Console.WriteLine(string.Format("{0:M}", dt));    //28 十二月
173 
174 Console.WriteLine(string.Format("{0:R}", dt));    //Mon, 28 Dec 2009 10:29:18 GMT
175 
176 Console.WriteLine(string.Format("{0:s}", dt));    //2009-12-28T10:29:18
177 
178 Console.WriteLine(string.Format("{0:t}", dt));    //AM 10:29
179 
180 Console.WriteLine(string.Format("{0:T}", dt));    //AM 10:29:18
181 
182 Console.WriteLine(string.Format("{0:u}", dt));    //2009-12-28 10:29:18Z
183 
184 Console.WriteLine(string.Format("{0:U}", dt));    //2009年12月28日 AM 2:29:18
185 
186 Console.WriteLine(string.Format("{0:Y}", dt));    //2009年12月
187 
188 Console.WriteLine(string.Format("{0}", dt));    //28/12/2009 AM 10:29:18
189 
190 Console.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt));    //200912281029182047
191 
192 计算2个日期之间的天数差
193 DateTime dt1 = Convert.ToDateTime("2007-8-1");    
194 DateTime dt2 = Convert.ToDateTime("2007-8-15");   
195 TimeSpan span = dt2.Subtract(dt1);              
196 int dayDiff = span.Days ;                    
197 
198 计算某年某月的天数
199 int days = DateTime.DaysInMonth(2009, 8);       
200 days = 31;                                      
201 
202 给日期增加一天、减少一天
203 DateTime dt =DateTime.Now;
204 dt.AddDays(1); //增加一天 dt本身并不改变
205 dt.AddDays(-1);//减少一天 dt本身并不改变

转载至http://www.cnblogs.com/Peter-Zhang/articles/1778143.html

posted @ 2015-11-02 17:08  笨笨的菜鸟  阅读(311)  评论(0编辑  收藏  举报