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
TimeSpan.FromMilliseconds() Method in C#
The TimeSpan.FromMilliseconds() method in C# is used to return a TimeSpan that represents a specified number of milliseconds. This static method is particularly useful when you need to create time intervals based on millisecond values, such as delays, timeouts, or performance measurements.
Syntax
Following is the syntax for the TimeSpan.FromMilliseconds() method −
public static TimeSpan FromMilliseconds(double value);
Parameters
The method accepts one parameter −
value − A
doublethat represents the number of milliseconds. It can be positive, negative, or zero.
Return Value
The method returns a TimeSpan object that represents the specified number of milliseconds. If the value is too large or too small to be represented as a TimeSpan, an OverflowException is thrown.
Using FromMilliseconds() with Different Values
Example
using System;
public class Demo {
public static void Main() {
TimeSpan span1 = TimeSpan.FromMilliseconds(1000); // 1 second
TimeSpan span2 = TimeSpan.FromMilliseconds(60000); // 1 minute
TimeSpan span3 = TimeSpan.FromMilliseconds(3600000); // 1 hour
TimeSpan span4 = TimeSpan.FromMilliseconds(500.5); // 500.5 milliseconds
Console.WriteLine("1000 ms = " + span1);
Console.WriteLine("60000 ms = " + span2);
Console.WriteLine("3600000 ms = " + span3);
Console.WriteLine("500.5 ms = " + span4);
Console.WriteLine("\nTotal milliseconds:");
Console.WriteLine("span1: " + span1.TotalMilliseconds);
Console.WriteLine("span4: " + span4.TotalMilliseconds);
}
}
The output of the above code is −
1000 ms = 00:00:01 60000 ms = 00:01:00 3600000 ms = 01:00:00 500.5 ms = 00:00:00.5005000 Total milliseconds: span1: 1000 span4: 500.5
Using FromMilliseconds() for Performance Timing
Example
using System;
public class Demo {
public static void Main() {
DateTime start = DateTime.Now;
// Simulate some work
for (int i = 0; i < 1000000; i++) {
Math.Sqrt(i);
}
DateTime end = DateTime.Now;
TimeSpan elapsed = end - start;
// Create equivalent TimeSpan using FromMilliseconds
TimeSpan fromMs = TimeSpan.FromMilliseconds(elapsed.TotalMilliseconds);
Console.WriteLine("Elapsed time: " + elapsed);
Console.WriteLine("From milliseconds: " + fromMs);
Console.WriteLine("Total milliseconds: " + elapsed.TotalMilliseconds);
// Check if delay is more than 100ms
TimeSpan threshold = TimeSpan.FromMilliseconds(100);
if (elapsed > threshold) {
Console.WriteLine("Operation took more than 100ms");
} else {
Console.WriteLine("Operation completed quickly");
}
}
}
The output of the above code is −
Elapsed time: 00:00:00.0156250 From milliseconds: 00:00:00.0156250 Total milliseconds: 15.625 Operation completed quickly
Working with Negative and Fractional Values
Example
using System;
public class Demo {
public static void Main() {
TimeSpan positive = TimeSpan.FromMilliseconds(2500);
TimeSpan negative = TimeSpan.FromMilliseconds(-1500);
TimeSpan fractional = TimeSpan.FromMilliseconds(1234.567);
TimeSpan zero = TimeSpan.FromMilliseconds(0);
Console.WriteLine("Positive (2500ms): " + positive);
Console.WriteLine("Negative (-1500ms): " + negative);
Console.WriteLine("Fractional (1234.567ms): " + fractional);
Console.WriteLine("Zero (0ms): " + zero);
Console.WriteLine("\nComponent breakdown for fractional:");
Console.WriteLine("Seconds: " + fractional.Seconds);
Console.WriteLine("Milliseconds: " + fractional.Milliseconds);
Console.WriteLine("Total Milliseconds: " + fractional.TotalMilliseconds);
}
}
The output of the above code is −
Positive (2500ms): 00:00:02.5000000 Negative (-1500ms): -00:00:01.5000000 Fractional (1234.567ms): 00:00:01.2345670 Zero (0ms): 00:00:00 Component breakdown for fractional: Seconds: 1 Milliseconds: 234 Total Milliseconds: 1234.567
Conclusion
The TimeSpan.FromMilliseconds() method provides a convenient way to create TimeSpan objects from millisecond values. It accepts both positive and negative values, including fractional milliseconds, making it ideal for precise timing operations and performance measurements.
