C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Finally: We convert that long Ticks back into a TimeSpan, showing that you can round-trip longs and TimeSpans.
C# program that converts TimeSpan to long
using System;
class Program
{
static void Main()
{
// Difference between today and yesterday.
DateTime yesterday = DateTime.Now.Subtract(TimeSpan.FromDays(1));
DateTime now = DateTime.Now;
TimeSpan diff = now.Subtract(yesterday);
// TimeSpan can be represented as a long [ticks].
long ticks = diff.Ticks;
// You can convert a long [ticks] back into TimeSpan.
TimeSpan ts = TimeSpan.FromTicks(ticks);
// Display.
Console.WriteLine(ts);
// Note: long and TimeSpan are the same number of bytes [8].
unsafe
{
Console.WriteLine(sizeof(long));
Console.WriteLine(sizeof(TimeSpan));
}
}
}
Output
1.00:00:00.0010000
8
8
Because: External systems often support simple numeric types better than struct types.