Beispiel #1
0
        static void Main(string[] args)
        {
            var timeSpan = new timeSpan(1, 2, 3);

            var timeSpan1 = new timeSpan(1, 0, 0);
            var timeSpan2 = TimeSpan.FromHours(1);

            var start    = DateTime.Now;
            var end      = DateTime.Now.AddMinutes(2);
            var duration = end - start;

            Console.WriteLine("Duration : " + duration);

            //properties
            Console.WriteLine("Minutes: " + timeSpan.Minutes);
            Console.WriteLine("Total Minutes: " + timeSpan.TotalMinutes);

            // add
            Console.WriteLine("Add Example: " + timeSpan.Add(TimeSpan.FromMinutes(8)));
            Console.WriteLine("Add Example: " + timeSpan.Subtract(TimeSpan.FromMinutes(2)));

            // ToString
            Console.WriteLine("ToString" + timeSpan.ToString());

            // Parse
            Console.WriteLine("Parse: " + TimeSpan.Parse("01:02:03"));
        }
Beispiel #2
0
 /// <summary>
 /// 计算时间与当前时间间隔
 /// </summary>
 /// <param name="date">日期和时间</param>
 /// <param name="ai_timeSpan"></param>
 /// <returns></returns>
 public static int DateDiff(DateTime date, timeSpan ai_timeSpan)
 {
     try
     {
         return DateDiff(date, DateTime.Now, ai_timeSpan);
     }
     catch
     {
         return 0;
     }
 }
Beispiel #3
0
 /// <summary>
 /// 计算两个日期的时间间隔
 /// </summary>
 /// <param name="DateTimeBegin">第一个日期和时间</param>
 /// <param name="DateTimeEnd">第二个日期和时间</param>
 /// <returns></returns>
 public static int DateDiff(string DateTimeBegin, string DateTimeEnd, timeSpan ai_timeSpan)
 {
     try
     {
         return DateDiff(Convert.ToDateTime(DateTimeBegin), Convert.ToDateTime(DateTimeEnd), ai_timeSpan);
     }
     catch
     {
         return 0;
     }
 }
Beispiel #4
0
 /// <summary>
 /// 计算时间与当前时间间隔
 /// </summary>
 /// <param name="DateTimeBegin">日期和时间</param>
 /// <returns></returns>
 public static int DateDiff(string dateTime, timeSpan ai_timeSpan)
 {
     if (dateTime.Length == 5)
     {
         dateTime = DateTime.Now.ToString("yyyy-MM-dd") + " " + dateTime;
     }
     try
     {
         return DateDiff(Convert.ToDateTime(dateTime), DateTime.Now, ai_timeSpan);
     }
     catch
     {
         return 0;
     }
 }
Beispiel #5
0
 public static int DateDiff(DateTime DateTimeBegin, DateTime DateTimeEnd, timeSpan ai_timeSpan)
 {
     try
     {
         string dateDiff = null;
         TimeSpan ts1 = new TimeSpan(DateTimeBegin.Ticks);
         TimeSpan ts2 = new TimeSpan(DateTimeEnd.Ticks);
         TimeSpan ts = ts1.Subtract(ts2).Duration();
         switch (ai_timeSpan.ToString())
         {
             case "Minutes":
                 dateDiff = ts.TotalMinutes.ToString();
                 break;
             case "Hours":
                 dateDiff = ts.TotalHours.ToString();
                 break;
             case "Days":
                 dateDiff = ts.TotalDays.ToString();
                 break;
             case "Milliseconds":
                 dateDiff = ts.TotalMilliseconds.ToString();
                 break;
             case "Seconds":
                 dateDiff = ts.TotalSeconds.ToString();
                 break;
             case "Ticks":
                 dateDiff = ts.Ticks.ToString();
                 break;
         }
         int n = 0;
         n = (int)Convert.ToDouble(dateDiff);
         return n;
     }
     catch
     {
         return 0;
     }
 }