Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Last active March 20, 2018 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs3339/2e0f561db89e31739774b1d649e9c0fa to your computer and use it in GitHub Desktop.
Save mjs3339/2e0f561db89e31739774b1d649e9c0fa to your computer and use it in GitHub Desktop.
C# Tick Time Date Constants and Conversion
[Serializable]
public class TicksToTime
{
////--------------Constants---------------
public const double TicksPerMicrosecond = 10;
private const double MicrosecondsPerTick = 1.0 / TicksPerMicrosecond;
public const long TicksPerMillisecond = 10000;
private const double MillisecondsPerTick = 1.0 / TicksPerMillisecond;
public const long TicksPerSecond = TicksPerMillisecond * 1000;
private const double SecondsPerTick = 1.0 / TicksPerSecond;
public const long TicksPerMinute = TicksPerSecond * 60;
private const double MinutesPerTick = 1.0 / TicksPerMinute;
public const long TicksPerHour = TicksPerMinute * 60;
private const double HoursPerTick = 1.0 / TicksPerHour;
public const long TicksPerDay = TicksPerHour * 24;
private const double DaysPerTick = 1.0 / TicksPerDay;
public const int MillisPerSecond = 1000;
public const int MillisPerMinute = MillisPerSecond * 60;
public const int MillisPerHour = MillisPerMinute * 60;
public const int MillisPerDay = MillisPerHour * 24;
private const long MaxMilliSeconds = long.MaxValue / TicksPerMillisecond;
private const long MinMilliSeconds = long.MinValue / TicksPerMillisecond;
public const long MicrosPerSecond = 1000000;
public const long MicrosPerMinute = MicrosPerSecond * 60;
public const long MicrosPerHour = MicrosPerMinute * 60;
public const long MicrosPerDay = MicrosPerHour * 24;
public static readonly TicksToTime MaxValue = new TicksToTime(long.MaxValue);
public static readonly TicksToTime MinValue = new TicksToTime(long.MinValue);
////--------------Constructor---------------
public TicksToTime(long ticks)
{
Ticks = ticks;
}
////--------------Individual Time Components---------------
public long Ticks { get; }
public int Days => (int) (Ticks / TicksPerDay);
public int Hours => (int) (Ticks / TicksPerHour % 24);
public int Milliseconds => (int) (Ticks / TicksPerMillisecond % 1000);
public int Microseconds => (int) (Ticks / TicksPerMicrosecond % 1000);
public int Minutes => (int) (Ticks / TicksPerMinute % 60);
public int Seconds => (int) (Ticks / TicksPerSecond % 60);
////--------------Total Times(Elapsed)---------------
public double TotalDays => Ticks * DaysPerTick;
public double TotalHours => Ticks * HoursPerTick;
public double TotalMinutes => Ticks * MinutesPerTick;
public double TotalSeconds => Ticks * SecondsPerTick;
public double TotalMilliseconds => Ticks * MillisecondsPerTick;
public double TotalMicroseconds => Ticks * MicrosecondsPerTick;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment