panipsilos
panipsilos

Reputation: 67

Convert from "TimeSpan" to "Long"

How can I convert a timespan datatype to a long?

Upvotes: 3

Views: 12328

Answers (3)

komizo
komizo

Reputation: 1061

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

Upvotes: 1

Guilherme Campos
Guilherme Campos

Reputation: 379

Actually you must use

MyTimeSpan.Ticks;

instead

MyTimeSpan.Ticks();

Upvotes: 4

M.A. Hanin
M.A. Hanin

Reputation: 8084

Assuming you use .NET, Use:

MyTimeSpan.Ticks()

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

MyTimeSpan = New TimeSpan(totalTicks)

Upvotes: 7

Related Questions