Question

How can I convert a timespan datatype to a long?

Was it helpful?

Solution

Assuming you use .NET, Use:

MyTimeSpan.Ticks()

and to convert back (sample in VB.NET, C# implementation is trivial nontheless):

MyTimeSpan = New TimeSpan(totalTicks)

OTHER TIPS

Actually you must use

MyTimeSpan.Ticks;

instead

MyTimeSpan.Ticks();

I don't know what you would to accomplish, but TimeSpan has some static fields:

    long ticks = TimeSpan.TicksPerDay;

    Console.WriteLine(ticks);
    Console.WriteLine(TimeSpan.TicksPerHour);
    Console.WriteLine(TimeSpan.TicksPerSecond);

you can also get another double values like:

TimeSpan nearlyFiveDays = TimeSpan.FromDays(5) - TimeSpan.FromSeconds(1);

Console.WriteLine(nearlyFiveDays.TotalDays);          // 4,99998842592593
Console.WriteLine(nearlyFiveDays.TotalHours);         // 119,999722222222
Console.WriteLine(nearlyFiveDays.TotalMinutes);       // 7199,98333333333
Console.WriteLine(nearlyFiveDays.TotalSeconds);       // 431999
Console.WriteLine(nearlyFiveDays.TotalMilliseconds);  // 431999000
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top