2013年6月4日 星期二

C# 日期相減

[C#]日期相減


  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. using System.Linq;
  6.  
  7. using System.Web;
  8.  
  9. using System.Web.UI;
  10.  
  11. using System.Web.UI.WebControls;
  12.  
  13.  
  14.  
  15. public partial class Test_tsDateSubtract : System.Web.UI.Page
  16.  
  17. {
  18.  
  19.     protected void Page_Load(object sender, EventArgs e)
  20.  
  21.     {
  22.  
  23.         DateTime StartTime = DateTime.Parse("2013-05-20"); //開始日
  24.  
  25.         //DateTime EndTime = DateTime.Parse("2013-05-21"); //指定結束日
  26.  
  27.         DateTime EndTime = DateTime.Now; //當日
  28.  
  29.         TimeSpan TS = EndTime.Subtract(StartTime); //日期相減
  30.  
  31.  
  32.  
  33.         Response.Write(TS.Days.ToString() + "<br>");         //天(int)
  34.  
  35.         Response.Write(TS.Hours.ToString() + "<br>");        //小時(int)
  36.  
  37.         Response.Write(TS.Minutes.ToString() + "<br>");      //分(int)
  38.  
  39.  
  40.  
  41.         Response.Write(TS.TotalDays.ToString() + "<br>");    //天(double)
  42.  
  43.         Response.Write(TS.TotalHours.ToString() + "<br>");   //小時(double)
  44.  
  45.         Response.Write(TS.TotalSeconds.ToString() + "<br>"); //分(double)
  46.  
  47.     }
  48.  
  49. }

沒有留言:

張貼留言