timespan usage details

  • 2020-06-15 10:06:53
  • OfStack

Some important USES:

a starts with a few methods

TimeSpan.Minutes(the difference in the number of minutes obtained when all other times, such as days, hours, and seconds, are one), the other Hours, Second1
DateTime.Tick: is one timing cycle, 1 nanosecond, that is, 1 millionth of a second, so Ticks here represents the total difference in time cycles, that is: 9 * 24 * 3600 * 10000000 + 23 * 3600 * 10000000 + 59 * 60 * 10000000 + 59 * 10000000 = 8639990000000. 3600 is the number of seconds in an hour
TimeSpan.TotalDays: the number of days between two time periods, the other TotalHours,TotalMinutes,TotalSeconds 1

b difference of two times


string time1 = "2010-5-26 8:10:00";
string time2 = "2010-5-26 18:20:00";
DateTime t1 = Convert.ToDateTime(time1);
DateTime t2 = Convert.ToDateTime(time2);

TimeSpan ts1=t2-t1;
string tsMin=ts1.Minutes.ToString();

 
TimeSpan ts11=new TimeSpan(t1.Tick);
TimeSpan ts22=new TimeSpan(t2.Tick);

string diff=ts22.Subtract(ts11).TotalMinutes.ToString();

Subtract: Represents the difference between two time periods
diff: This is the number of minutes between the two times. The above example is 610 minutes.

Get an instance of TimeSpan. TimeSpan has 1 property: Days, TotalDays, Hours, TotalHours, Minutes, TotalMinutes, Seconds, TotalSeconds, Ticks. Note that there is no TotalTicks.
These attribute names are a little difficult to understand at first, but after reading this article, you'll have a better idea.

For example

Time 1 is 2010-1-2-8:43:35;
Time 2 is 2010-1-12 8:43:34.
Take time 2 minus time 1 to get an instance of TimeSpan.

So time 2 is 9 days, 23 hours, 59 minutes and 59 seconds longer than time 1.

So Days is 9, Hours is 23, Minutes is 59, Seconds is 59.

So in the future it's much easier to know the difference between the two time periods

TimeSpan Format Helper


using System;
using System.Collections.Generic;
class TimeSpanUtility
{
    public static string FormatString(TimeSpan aTimeSpan)
    {
        string newFormat = aTimeSpan.ToString("d'd 'h'h 'm'm 's's'");
        // 1d 3h 43m 23s
        return newFormat;
    }
    public static string TimeSpanInWords(TimeSpan aTimeSpan)
    {
        List<string> timeStrings = new List<string>();
        int[] timeParts = new[] { aTimeSpan.Days, aTimeSpan.Hours, aTimeSpan.Minutes, aTimeSpan.Seconds };
        string[] timeUnits = new[] { "day", "hour", "minute", "second" };
        for (int i = 0; i < timeParts.Length; i++)
        {
            if (timeParts[i] > 0)
            {
                timeStrings.Add(string.Format("{0} {1}", timeParts[i], Pluralize(timeParts[i], timeUnits[i])));
            }
        }
        return timeStrings.Count != 0 ? string.Join(", ", timeStrings.ToArray()) : "0 seconds";
    }
    private static string Pluralize(int n, string unit)
    {
        if (string.IsNullOrEmpty(unit)) return string.Empty;
        n = Math.Abs(n); // -1 should be singular, too
        return unit + (n == 1 ? string.Empty : "s");
    }
}
public class Client
{
    static void Main()
    {
        // 12 days, 23 hours, 24 minutes, 2 seconds.
        TimeSpan span = new TimeSpan(12, 23, 24, 2);
        Console.WriteLine(TimeSpanUtility.TimeSpanInWords(span));   // Output: 12 days, 23 hours, 24 minutes, 2 seconds
        Console.WriteLine(TimeSpanUtility.FormatString(span));  // Output: 12d 23h 24m 2s
    }
}


Related articles: