在项目开发中,常常会碰到日期处理。好比查询中,可能会常常遇到按时间段查询,有时会默认取出一个月的数据。当咱们提交数据时,会须要记录当前日期,等等。下面就看看一些经常使用的方法。ide
首先,DateTime是一个struct。不少时候,会把它当成一个类。但它真的不是,MSDN上的描述以下:spa
DateTime结构:表示时间上的一刻,一般以日期和当天的时间表示。语法:code
[SerializableAttribute] public struct DateTime : IComparable, IFormattable, IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime>
MSDN链接:MSDN DateTime结构orm
1、DateTime.Now属性对象
实例化一个DateTime对象,能够将指定的数字做为年月日获得一个DateTime对象。而DateTime.Now属性则可得到当前时间。若是你想按年、月、日分别统计数据,也可用DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day获取。同理,当前的时分秒也能够这样的方式获取。还能够在当前时间加上一个段时间等操做。blog
static void Main(string[] args) { DateTime newChina = new DateTime(1949, 10, 1); Console.WriteLine(newChina); Console.WriteLine("当前时间:"); Console.WriteLine("{0}年,{1}月,{2}日",DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); Console.WriteLine("{0}时,{1}分, {2}秒",DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); Console.WriteLine("三天后:{0}",DateTime.Now.AddDays(3)); Console.ReadLine(); }
结果:开发
2、ToString方法字符串
DateTime的ToString方法有四种重载方式。其中一个重载方式容许传入String,这就意味着你能够将当前DateTime对象转换成等效的字符串形式。好比咱们将当前时间输出,日期按yyyy-mm-dd格式,时间按hh:mm:ss格式。get
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd")); Console.WriteLine(DateTime.Now.ToString("hh:mm:ss"));
还有一个重载形式是须要提供IFormatProvider,使用指定的区域性特定格式信息将当前 DateTime 对象的值转换为它的等效字符串表示形式。源码
static void Main(string[] args) { CultureInfo jaJP = new CultureInfo("ja-JP"); jaJP.DateTimeFormat.Calendar = new JapaneseCalendar(); DateTime date1 = new DateTime(1867, 1, 1); DateTime date2 = new DateTime(1967, 1, 1); try { Console.WriteLine(date2.ToString(jaJP)); Console.WriteLine(date1.ToString(jaJP)); } catch (ArgumentOutOfRangeException) { Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}", date1, jaJP.DateTimeFormat.Calendar.MinSupportedDateTime, jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime); } Console.ReadLine(); }
结果:
没太明白,日本历史那么短?百度了一下1868年9月8日,明治维新之后了。
付DateTimeFormatInfo类, 这里有比较全的时间日期格式对应的字符串。
3、DaysInMonth方法及IsLeapYear方法
DaysInMonth方法须要两个Int32型参数,返回指定年份指定月份的天数。关于月份的天数,多数只有2月须要特殊照顾一下。剩余的月份,不管哪一年的天数都是固定的。而二月呢,不但不是其余月份的30天或31天,她还分个闰年非闰年。
static void Main(string[] args) { Console.WriteLine("2000年至2015年中二月的天数"); for (int i = 2000; i < 2015; i++) { Console.WriteLine("{0}年2月有:{1}天", i, DateTime.DaysInMonth(i, 2)); } Console.ReadLine(); }
输出结果:
从输出结果中能够看出,2月为29天的年份为闰年。但其实DateTime还提供了判断闰年的方法IsLeapYear,该方法只要一个Int32的参数,若输入的年份是闰年返回true,不然返回false。(.Net Framework就是这么贴心,你要的东西都给你封装好了,直接拿来用好了。)要是没这个方法呢,得本身去按照闰年的规则去写个小方法来判断。
static void Main(string[] args) { Console.WriteLine("2000年至2015年中二月的天数"); for (int i = 2000; i < 2015; i++) { if (DateTime.IsLeapYear(i)) Console.WriteLine("{0}年是闰年,2月有{1}天", i, DateTime.DaysInMonth(i, 2)); else Console.WriteLine("{0}年是平年,2月有{1}天",i,DateTime.DaysInMonth(i,2)); } Console.ReadLine(); }
微软如今已经将.NetFramework开源了,这意味着能够本身去查看源代码了。附上DateTime.cs的源码连接,以及IsLeapYear方法的源代码。虽然仅仅两三行代码,但在实际开发中,你可能一时间想不起闰年的计算公式,或者拿捏不许。封装好的方法为你节省大量时间。
// Checks whether a given year is a leap year. This method returns true if // year is a leap year, or false if not. // public static bool IsLeapYear(int year) { if (year < 1 || year > 9999) { throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_Year")); } Contract.EndContractBlock(); return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); }
总结
介绍了几个算是比较经常使用的方法。在本身的项目中遇到日期操做的时候,多看看.NetFramework给咱们提供了什么样的方法。不少时候用所提供的方法拼凑一下,就能轻易的获取到咱们想要的日期或者时间。