C#關於時間處理

來源:互聯網
上載者:User

一. DateTime

在C#中,系統提供了許多關於時間處理的函數,我們可以通過System.DateTime.Now擷取系統時間.當然也可以用DateTime類來建立時間對象.下面給了幾個例子擷取系統時間的方法.

DateTime dt1 = new DateTime(2004, 10, 19);

Console.WriteLine("dt1: {0}", dt1);

DateTime dt2 = new DateTime(2004, 10, 19, 22, 47, 35);

Console.WriteLine("dt2: {0}", dt2);

DateTime dt3 = new DateTime(2004, 10, 19, 22, 47, 35, 259);

Console.WriteLine("dt3: {0}", dt3);

Stringtime1= System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss");

Console.WriteLine(time1);

程式啟動並執行結果截圖是

 

System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss")的意思是擷取系統目前時間,並以年-月-日-時-分-秒的格式顯示出來.

再看下一個有管時間的類:Timespan,就字面的意思應該知道它的中文意思時間間隔.先看一個例子:

TimeSpan oneweek = new TimeSpan(4, 12, 0, 0) + 

new TimeSpan(2, 12, 0, 0);

//(4, 12, 0, 0)中的參數分別表示

days,hours,minutes,seconds.

DateTime now = DateTime.Now;

DateTime past = now - oneweek;

Console.WriteLine(oneweek.ToString());

Console.WriteLine(now.ToString());

Console.WriteLine(past.ToString());

輸出結果時

 

.在當前的時間上加上X秒./分/時/天等操作.

DateTime time1 = DateTime.Now;

DateTime time2 = DateTime.Now.AddYears(10);

DateTime time3 = DateTime.Now.AddMonths(11);

DateTime time4 = DateTime.Now.AddDays(7);

DateTime time5 = DateTime.Now.AddHours(12);

Console.WriteLine(time1.ToString());

Console.WriteLine(time2.ToString());

Console.WriteLine(time3.ToString());

Console.WriteLine(time4.ToString());

Console.WriteLine(time5.ToString());

程式啟動並執行結果截圖是:

 

DateTime time2 = DateTime.Now.AddYears(10)的意思是把目前時間上加上10年後再賦值給 time2;

有關本地時間和標準UTC時間: UTC時間也就是格林威治標準時間.

DateTime local=DateTime.Now;//本地時間

Console.WriteLine("{0},{1},{2}",local.ToString(),local.Kind.ToString(),TimeZone.CurrentTimeZone.StandardName);

DateTime utctime=DateTime.UtcNow;//協調通用時間;

Console.WriteLine("{0},{1}",utctime.ToString(),utctime.Kind.ToString());

 

 

二.有關時間格式的問題

時間顯示的格式可以是24或者12小時制的,還有其他方式的.

DateTime local=DateTime.Now;//本地時間

Console.WriteLine("the time is{0: hh:mm tt}",local);

Console.WriteLine("Date is {0:ddd MMM dd, yyyy}", local);

Console.WriteLine("{0:ddd MMM dd,yyyy}",local);

Console.WriteLine("{0: HH :mm :ss tt}",local);

Console.WriteLine("{0 :m}",local);

 

TimeSpan在有關時間處理的過程有很多的作用,例如計算兩個時間段之間的時間有多久,或者1個月後的今天是幾號等.

TimeSpan的建構函式有:

TimeSpan oneweek = new TimeSpan(4, 12, 0, 0) + new TimeSpan(2, 12, 0, 0);

那麼oneweek就等於7天:4.5天加2.5天

TimeSpan tp = new TimeSpan(1,50,20,10);//dd hh mm ss

TimeSpan tp2 = new TimeSpan(1, 50, 20);// hh mm ss

TimeSpan tp3 = new TimeSpan(1,1,1,1,1);//dd hh mm ss msms

//(4, 12, 0, 0)中的參數分別表示days,hours,minutes,seconds.

同時我們可以用TimeSpan的靜態方法來給TimeSpan的執行個體對象賦值

請看下面的例子:

TimeSpan spa1 = TimeSpan.FromDays(10);

TimeSpan spa2 = TimeSpan.FromHours(10);

TimeSpan spa3 = TimeSpan.FromMinutes(44);

TimeSpan spa4 = TimeSpan.FromSeconds(5);

TimeSpan spa5 = TimeSpan.FromMilliseconds(55);

TimeSpan spa6 = TimeSpan.FromTicks(200);//時間的刻度

Console.WriteLine(spa1);

Console.WriteLine(spa2);

Console.WriteLine(spa3);

Console.WriteLine(spa4);

Console.WriteLine(spa5);

Console.WriteLine(spa6);

輸出結果是:

 

下一節將介紹C#中的Array和ArrayList.等知識.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.