Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# TicksPer constants
The TicksPer constants in C# are predefined values in the TimeSpan class that represent the number of ticks for different time units. A tick is the smallest unit of time measurement in .NET, where one tick equals 100 nanoseconds. These constants help convert between ticks and common time measurements.
Available TicksPer Constants
The TimeSpan class provides the following TicksPer constants −
TicksPerDay− Number of ticks in one dayTicksPerHour− Number of ticks in one hourTicksPerMinute− Number of ticks in one minuteTicksPerSecond− Number of ticks in one secondTicksPerMillisecond− Number of ticks in one millisecond
Using All TicksPer Constants
Example
using System;
public class Demo {
public static void Main() {
Console.WriteLine("TicksPer Constants:");
Console.WriteLine("TicksPerDay: " + TimeSpan.TicksPerDay);
Console.WriteLine("TicksPerHour: " + TimeSpan.TicksPerHour);
Console.WriteLine("TicksPerMinute: " + TimeSpan.TicksPerMinute);
Console.WriteLine("TicksPerSecond: " + TimeSpan.TicksPerSecond);
Console.WriteLine("TicksPerMillisecond: " + TimeSpan.TicksPerMillisecond);
}
}
The output of the above code is −
TicksPer Constants: TicksPerDay: 864000000000 TicksPerHour: 36000000000 TicksPerMinute: 600000000 TicksPerSecond: 10000000 TicksPerMillisecond: 10000
Practical Application
Example
using System;
public class Demo {
public static void Main() {
long totalTicks = DateTime.Now.Ticks;
// Convert ticks to different time units
long days = totalTicks / TimeSpan.TicksPerDay;
long hours = (totalTicks % TimeSpan.TicksPerDay) / TimeSpan.TicksPerHour;
long minutes = (totalTicks % TimeSpan.TicksPerHour) / TimeSpan.TicksPerMinute;
Console.WriteLine("Current DateTime in ticks: " + totalTicks);
Console.WriteLine("Days since January 1, 0001: " + days);
Console.WriteLine("Remaining hours: " + hours);
Console.WriteLine("Remaining minutes: " + minutes);
// Create TimeSpan using ticks
TimeSpan ts = new TimeSpan(5 * TimeSpan.TicksPerHour + 30 * TimeSpan.TicksPerMinute);
Console.WriteLine("TimeSpan created: " + ts);
}
}
The output of the above code is −
Current DateTime in ticks: 638679542400000000 Days since January 1, 0001: 739230 Remaining hours: 4 Remaining minutes: 0 TimeSpan created: 05:30:00
Conclusion
TicksPer constants in C# provide a convenient way to work with time measurements at the tick level. These constants are essential for precise time calculations, converting between different time units, and creating TimeSpan objects with specific durations based on ticks.
