C#中比擬經常使用的DateTime構造的應用辦法。本站提示廣大學習愛好者:(C#中比擬經常使用的DateTime構造的應用辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中比擬經常使用的DateTime構造的應用辦法正文
在項目開辟中,常常會碰著日期處置。好比查詢中,能夠會常常碰到按時光段查詢,有時會默許掏出一個月的數據。當我們提交數據時,會須要記載以後日期,等等。上面就看看一些經常使用的辦法。
起首,DateTime是一個struct。許多時刻,會把它當做一個類。但它真的不是,MSDN上的描寫以下:
DateTime構造:表現時光上的一刻,平日以日期和當天的時光表現。語法:
[SerializableAttribute] public struct DateTime : IComparable, IFormattable, IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime>
1、DateTime.Now屬性
實例化一個DateTime對象,可以將指定的數字作為年代日獲得一個DateTime對象。而DateTime.Now屬性則可取得以後時光。假如你想按年、月、日分離統計數據,也可用DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day獲得。同理,以後的時分秒也能夠如許的方法獲得。還可以在以後時光加上一個段時光等操作。
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格局。
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();
}
成果:

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辦法的源代碼。固然僅僅兩三行代碼,但在現實開辟中,你能夠一時光想不起閏年的盤算公式,或許拿捏禁絕。封裝好的辦法為你節儉年夜量時光。
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);
}
文章中引見了幾個算是比擬經常使用的辦法,願望對年夜家的進修有所贊助,日常平凡多浏覽相干文章,積聚經歷。