.net对时间的操作(详解)
2010-01-13 20:14
183 查看
DateTime相关属性
DateTime日期=DateTime.Today;//'Today'获取当前日期
Response.Write(日期);
//显示2010-1-130:00:00
DateTimedt=DateTime.Now;//'Now'获取当前时间包括年月日时分秒
Response.Write(dt.ToString());
//显示2010-1-1320:36:12
Response.Write(dt.Date.ToString());//'Date'获取当前实例dt的日期部分
//显示2010-1-130:00:00
Response.Write(dt.Day.ToString());//'Day'获取当前实例dt所表示是的日期为该月中的第几天,即日
//显示13
Response.Write(dt.DayOfWeek.ToString());//'DayOfWeek'获取当前实例dt所表示的日期是星期几
//显示Wednesday
Response.Write(dt.DayOfYear.ToString());//'DayOfYear'获取当前实例dt所表示的日期是一年中的第几天
//显示13
Response.Write(dt.Hour.ToString());//'Hour'获取当前实例dt所表示日期的小时部分
//显示20
Response.Write(dt.Millisecond.ToString());//'Milliscond'获取当前实例dt所表示日期的毫秒部分
//显示687
Response.Write(dt.Minute.ToString());//'Minute'获取当前实例dt所表示日期的分钟部分
//显示36
Response.Write(dt.Month.ToString());//'Month'获取当前实例dt所表示日期的月份
//显示1
Response.Write(dt.Second.ToString());//'Second'获取当前实例dt所表示日期的秒
//显示12
Response.Write(dt.TimeOfDay.ToString());//'TimeOfDay'获取当前实例dt所表示日期的时间部分
//显示20:36:12.6875000
Response.Write(dt.Year.ToString());//'Year'获取当前实例dt所表示日期的的年份
//显示2010
DateTime相关方法:
1.Add方法:将指定的TimeSpan的值加到实例的值上
publicstaticintCompare(
DateTimet1,
DateTimet2
)
[/code]
[/code]
DateTime日期=DateTime.Today;//'Today'获取当前日期
Response.Write(日期);
//显示2010-1-130:00:00
DateTimedt=DateTime.Now;//'Now'获取当前时间包括年月日时分秒
Response.Write(dt.ToString());
//显示2010-1-1320:36:12
Response.Write(dt.Date.ToString());//'Date'获取当前实例dt的日期部分
//显示2010-1-130:00:00
Response.Write(dt.Day.ToString());//'Day'获取当前实例dt所表示是的日期为该月中的第几天,即日
//显示13
Response.Write(dt.DayOfWeek.ToString());//'DayOfWeek'获取当前实例dt所表示的日期是星期几
//显示Wednesday
Response.Write(dt.DayOfYear.ToString());//'DayOfYear'获取当前实例dt所表示的日期是一年中的第几天
//显示13
Response.Write(dt.Hour.ToString());//'Hour'获取当前实例dt所表示日期的小时部分
//显示20
Response.Write(dt.Millisecond.ToString());//'Milliscond'获取当前实例dt所表示日期的毫秒部分
//显示687
Response.Write(dt.Minute.ToString());//'Minute'获取当前实例dt所表示日期的分钟部分
//显示36
Response.Write(dt.Month.ToString());//'Month'获取当前实例dt所表示日期的月份
//显示1
Response.Write(dt.Second.ToString());//'Second'获取当前实例dt所表示日期的秒
//显示12
Response.Write(dt.TimeOfDay.ToString());//'TimeOfDay'获取当前实例dt所表示日期的时间部分
//显示20:36:12.6875000
Response.Write(dt.Year.ToString());//'Year'获取当前实例dt所表示日期的的年份
//显示2010
DateTime相关方法:
1.Add方法:将指定的TimeSpan的值加到实例的值上
publicDateTimeAdd( TimeSpanvalue ) 参数TimeSpan包含要添加的时间间隔;对象表示时间间隔或持续时间,按正负天数、小时数、分钟数、秒数以及秒的小数部分进行度量。
如:DateTimedt=DateTime.Now; TimeSpants=newTimeSpan(1,1,1,1);//参数表示1天,1小时,1分钟,1秒,1毫秒 Response.Write(dt.Add(ts).ToString());
2.AddDays方法:将指定的天数加到实例的值上
publicDateTimeAddDays( doublevalue )
如:DateTimedt=DateTime.Now;
Response.Write(dt.AddDays(30).ToString());//多余的部分按顺序加到前面的单位
3.AddHours:将指定的小时加到实例的值上
4.AddMilliseconds:将指定的毫秒数加到实例的值上
5.AddMinute:同上
6.AddMonths:同上
7.AddSeconds:同上
8.AddYears:同上
9.Compare:比较DateTime的两个实例,并返回他们的相对值的指示。
)
实例如下:DateTimedt1=newDateTime(100);
DateTimedt2=newDateTime(200);
if(DateTime.Compare(dt1,dt2)>0)
Response.Write("dt1>dt2");
if(DateTime.Compare(dt1,dt2)==0)
Response.Write("dt1=dt2");
if(DateTime.Compare(dt1,dt2)<0)
Response.Write("dt1<dt2");
10.CompareTo:将此实例与指定对象进行比较并返回一个对二者的相对值(int)的指示。
如:DateTimedt1=newDateTime(100);
DateTimedt2=newDateTime(200);
inti=dt1.CompareTo(dt2);
Response.Write(i);
11.DaysInMonth:返回指定年和月中的天数。
如:inti=DateTime.DaysInMonth(2010,1);
Response.Write(i);
12.Equals方法:返回一个值,该值指示此实例是否与指定的DateTime实例相等。返回bool(true或false)
如:DateTimedt1=DateTime.Now.AddDays(1);
DateTimedt2=DateTime.Now;
Response.Write(dt1.Equals(dt2));
13.ToLongDateString将此实例的值转换为其等效的长日期字符串表示形式。
14.ToLongTimeString将此实例的值转换为其等效的长时间字符串表示形式。
15.ToShortDateString将此实例的值转换为其等效的短日期字符串表示形式。
16.ToShortTimeString将此实例的值转换为其等效的短时间字符串表示形式。
[/code]
[/code]
相关文章推荐
- .NET基础 (12)时间的操作System.DateTime
- Linux下使用timedatectl命令时间时区操作详解
- Linux下使用timedatectl命令时间时区操作详解
- 详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
- Linux 日期和时间操作详解
- NSDate详解及获取当前时间等常用操作
- java中获取当前时间以及对当前时间操作详解
- .net杂记 (调用EXE 获取路径 target VB DIV C# Split 页面之间传递值 更改session时间 EXCEL操作 控制电子邮件Mail input记录值的窍门)
- 【Linux 驱动】第七章 时间 延迟及延缓操作 (例子详解一)
- 详解JAVA常用的时间操作【实用】
- 详解免费高效实用的.NET操作Excel组件NPOI(.NET组件介绍之六)
- strftime操作时间详解
- .NET中如何操作数字证书详解
- mysql TIMESTAMP(时间戳)详解——查询最近一段时间操作的记录
- .Net中处理两个日期相差时间操作
- lock Mutex Monitor 之间的区别与详解, .net 多线程 同步异步操作,锁
- NSDate详解及获取当前时间等常用操作
- .net 时间操作[摘抄]
- mysql日期时间操作函数详解
- ElasticSearch第五步-.net平台下c#操作ElasticSearch详解