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# Program to get the absolute value of the time
To get the absolute value of time, use the TimeSpan.Duration()Duration() method returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.
When working with negative time spans, the Duration() method converts all negative components to their positive equivalents, effectively giving you the absolute time duration.
Syntax
Following is the syntax for the Duration() method −
public TimeSpan Duration()
Return Value
The method returns a new TimeSpan object that represents the absolute value of the current TimeSpan. If the current TimeSpan is already positive or zero, it returns an equivalent positive TimeSpan.
Using Duration() Method
Example
Let's create a TimeSpan with negative values and get its absolute value −
using System;
public class Demo {
public static void Main() {
TimeSpan ts = new TimeSpan(-7, -50, -25);
Console.WriteLine("Original TimeSpan: " + ts);
TimeSpan duration = ts.Duration();
Console.WriteLine("Absolute value: " + duration);
}
}
The output of the above code is −
Original TimeSpan: -07:50:25 Absolute value: 07:50:25
Comparing Positive and Negative TimeSpans
Example
using System;
public class Demo {
public static void Main() {
TimeSpan positiveTime = new TimeSpan(2, 30, 45);
TimeSpan negativeTime = new TimeSpan(-2, -30, -45);
Console.WriteLine("Positive TimeSpan: " + positiveTime);
Console.WriteLine("Its Duration: " + positiveTime.Duration());
Console.WriteLine("Negative TimeSpan: " + negativeTime);
Console.WriteLine("Its Duration: " + negativeTime.Duration());
Console.WriteLine("Are durations equal? " +
(positiveTime.Duration() == negativeTime.Duration()));
}
}
The output of the above code is −
Positive TimeSpan: 02:30:45 Its Duration: 02:30:45 Negative TimeSpan: -02:30:45 Its Duration: 02:30:45 Are durations equal? True
Common Use Cases
The Duration() method is particularly useful when −
-
Calculating elapsed time between two dates where the result might be negative
-
Finding the absolute difference between two time periods
-
Converting negative time spans to positive for display or calculation purposes
Example
using System;
public class Demo {
public static void Main() {
DateTime startTime = new DateTime(2023, 10, 15, 14, 30, 0);
DateTime endTime = new DateTime(2023, 10, 15, 12, 15, 30);
TimeSpan difference = endTime - startTime;
Console.WriteLine("Time difference: " + difference);
Console.WriteLine("Absolute duration: " + difference.Duration());
Console.WriteLine("Total minutes (absolute): " +
Math.Abs(difference.TotalMinutes) + " minutes");
}
}
The output of the above code is −
Time difference: -02:14:30 Absolute duration: 02:14:30 Total minutes (absolute): 134.5 minutes
Conclusion
The TimeSpan.Duration() method provides an easy way to get the absolute value of a time span by converting negative time components to positive ones. This is essential when you need to work with time differences regardless of their direction or when displaying elapsed time durations.
