C#時間比較

來源:互聯網
上載者:User
前一段時間需要比較時間,本來想DateTime.Compare( dt1, dt2 ),但是為了方便我得到的數值是string的,還得和目前時間進行比較,比如我寫12:00:00,如果Convert.ToDateTime後是帶有年月日而且全是零!!。後來想我只是比較時刻或者說shortDateTime,所以換了一種思路比較時刻。
例子:比較是上午還是下午。
           TimeSpan minDay = new TimeSpan(12, 0, 0);//定義一個中午時間
           TimeSpan time2 = TimeSpan.Zero;//
           TimeSpan.TryParse(time, out time2);//time是string的時間,把它轉換成timespan的
           if (time2 != TimeSpan.Zero)
           if (time2.CompareTo(minDay) < 0)//上午

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.