首页 > 编程 > C# > 正文

C#中比较常用的DateTime结构的使用方法

2019-10-29 21:35:52
字体:
来源:转载
供稿:网友
这篇文章主要介绍了C#中比较常用的DateTime结构的使用方法,需要的朋友可以参考下
 

在项目开发中,经常会碰到日期处理。比如查询中,可能会经常遇到按时间段查询,有时会默认取出一个月的数据。当我们提交数据时,会需要记录当前日期,等等。下面就看看一些常用的方法

首先,DateTime是一个struct。很多时候,会把它当成一个类。但它真的不是,MSDN上的描述如下:

DateTime结构:表示时间上的一刻,通常以日期和当天的时间表示。语法:
 

  1. [SerializableAttribute] 
  2. public struct DateTime : IComparable, IFormattable,  
  3.   IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime> 
?

一、DateTime.Now属性

实例化一个DateTime对象,可以将指定的数字作为年月日得到一个DateTime对象。而DateTime.Now属性则可获得当前时间。如果你想按年、月、日分别统计数据,也可用DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day获取。同理,当前的时分秒也可以这样的方式获取。还可以在当前时间加上一个段时间等操作。    
 

  1. static void Main(string[] args) 
  2.     { 
  3.       DateTime newChina = new DateTime(1949, 10, 1); 
  4.       Console.WriteLine(newChina); 
  5.       Console.WriteLine("当前时间:"); 
  6.       Console.WriteLine("{0}年,{1}月,{2}日",DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); 
  7.       Console.WriteLine("{0}时,{1}分, {2}秒",DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); 
  8.       Console.WriteLine("三天后:{0}",DateTime.Now.AddDays(3)); 
  9.       Console.ReadLine(); 
  10.     } 
?

结果:

C#中比较常用的DateTime结构的使用方法

二、ToString方法

 DateTime的ToString方法有四种重载方式。其中一个重载方式允许传入String,这就意味着你可以将当前DateTime对象转换成等效的字符串形式。比如我们将当前时间输出,日期按yyyy-mm-dd格式,时间按hh:mm:ss格式。
 

  1. Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd")); 
  2.   Console.WriteLine(DateTime.Now.ToString("hh:mm:ss")); 
?

还有一个重载形式是需要提供IFormatProvider,使用指定的区域性特定格式信息将当前 DateTime 对象的值转换为它的等效字符串表示形式。
 

  1. static void Main(string[] args) 
  2.     { 
  3.       CultureInfo jaJP = new CultureInfo("ja-JP"); 
  4.       jaJP.DateTimeFormat.Calendar = new JapaneseCalendar(); 
  5.       DateTime date1 = new DateTime(1867, 1, 1); 
  6.       DateTime date2 = new DateTime(1967, 1, 1); 
  7.        
  8.       try 
  9.       { 
  10.         Console.WriteLine(date2.ToString(jaJP)); 
  11.         Console.WriteLine(date1.ToString(jaJP)); 
  12.       } 
  13.       catch (ArgumentOutOfRangeException) 
  14.       { 
  15.         Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}"
  16.                  date1, 
  17.                  jaJP.DateTimeFormat.Calendar.MinSupportedDateTime, 
  18.                  jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime); 
  19.       } 
  20.       Console.ReadLine(); 
  21.     } 
?

结果:

C#中比较常用的DateTime结构的使用方法

三、DaysInMonth方法及IsLeapYear方法

 DaysInMonth方法需要两个Int32型参数,返回指定年份指定月份的天数。关于月份的天数,多数只有2月需要特殊照顾一下。剩余的月份,无论哪一年的天数都是固定的。而二月呢,不但不是其他月份的30天或31天,她还分个闰年非闰年。
 

  1. static void Main(string[] args) 
  2.     { 
  3.       Console.WriteLine("2000年至2015年中二月的天数"); 
  4.       for (int i = 2000; i < 2015; i++) 
  5.       { 
  6.         Console.WriteLine("{0}年2月有:{1}天", i, DateTime.DaysInMonth(i, 2)); 
  7.       } 
  8.       Console.ReadLine(); 
  9.     } 
?

输出结果:

C#中比较常用的DateTime结构的使用方法

从输出结果中可以看出,2月为29天的年份为闰年。但其实DateTime还提供了判断闰年的方法IsLeapYear,该方法只要一个Int32的参数,若输入的年份是闰年返回true,否则返回false。(.Net Framework就是这么贴心,你要的东西都给你封装好了,直接拿来用好了。)要是没这个方法呢,得自己去按照闰年的规则去写个小方法来判断。
 

  1. static void Main(string[] args) 
  2.     { 
  3.       Console.WriteLine("2000年至2015年中二月的天数"); 
  4.       for (int i = 2000; i < 2015; i++) 
  5.       { 
  6.         if (DateTime.IsLeapYear(i)) 
  7.           Console.WriteLine("{0}年是闰年,2月有{1}天", i, DateTime.DaysInMonth(i, 2)); 
  8.         else 
  9.           Console.WriteLine("{0}年是平年,2月有{1}天",i,DateTime.DaysInMonth(i,2)); 
  10.       } 
  11.       Console.ReadLine(); 
  12.     } 
?

微软现在已经将.NetFramework开源了,这意味着可以自己去查看源代码了。附上DateTime.cs的源码链接,以及IsLeapYear方法的源代码。虽然仅仅两三行代码,但在实际开发中,你可能一时间想不起闰年的计算公式,或者拿捏不准。封装好的方法为你节省大量时间。

DateTime.cs源码中IsLeapYear方法
 

  1. // Checks whether a given year is a leap year. This method returns true if 
  2.  // year is a leap year, or false if not. 
  3.  // 
  4.  public static bool IsLeapYear(int year) { 
  5.    if (year < 1 || year > 9999) { 
  6.      throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_Year")); 
  7.    } 
  8.    Contract.EndContractBlock(); 
  9.    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); 
  10.  } 
?

文章中介绍了几个算是比较常用的方法,希望对大家的学习有所帮助,平时多阅读相关文章,积累经验。



注:相关教程知识阅读请移步到c#教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表