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
DateTimeOffset.AddSeconds() Method in C#
The DateTimeOffset.AddSeconds() method in C# returns a new DateTimeOffset object that adds a specified number of whole and fractional seconds to the value of the current instance. This method is useful for time calculations where you need to preserve timezone offset information.
Syntax
Following is the syntax −
public DateTimeOffset AddSeconds(double val);
Parameters
val − A double representing the number of seconds to add. It can be positive (to add seconds) or negative (to subtract seconds). Fractional values are allowed.
Return Value
Returns a new DateTimeOffset object whose value is the sum of the date and time represented by the current instance and the number of seconds represented by val.
Adding Seconds to DateTimeOffset
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 10, 11, 7, 10, 20, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset (before adding seconds) = {0}", dateTimeOffset);
DateTimeOffset res = dateTimeOffset.AddSeconds(30);
Console.WriteLine("DateTimeOffset (after adding seconds) = {0}", res);
}
}
The output of the above code is −
DateTimeOffset (before adding seconds) = 10/11/2019 7:10:20 AM -05:00 DateTimeOffset (after adding seconds) = 10/11/2019 7:10:50 AM -05:00
Subtracting Seconds from DateTimeOffset
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 11, 11, 8, 20, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset (before subtracting seconds) = {0}", dateTimeOffset);
DateTimeOffset res = dateTimeOffset.AddSeconds(-10);
Console.WriteLine("DateTimeOffset (after subtracting seconds) = {0}", res);
}
}
The output of the above code is −
DateTimeOffset (before subtracting seconds) = 11/11/2019 8:20:10 AM -05:00 DateTimeOffset (after subtracting seconds) = 11/11/2019 8:20:00 AM -05:00
Using Fractional Seconds
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTime = new DateTimeOffset(2023, 5, 15, 14, 30, 45, 500, new TimeSpan(2, 0, 0));
Console.WriteLine("Original: {0}", dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff zzz"));
DateTimeOffset result = dateTime.AddSeconds(2.75);
Console.WriteLine("After adding 2.75 seconds: {0}", result.ToString("yyyy-MM-dd HH:mm:ss.fff zzz"));
}
}
The output of the above code is −
Original: 2023-05-15 14:30:45.500 +02:00 After adding 2.75 seconds: 2023-05-15 14:30:48.250 +02:00
Conclusion
The DateTimeOffset.AddSeconds() method provides precise time manipulation while preserving timezone information. It accepts both positive and negative values, including fractional seconds, making it versatile for various time calculation scenarios in applications that require timezone-aware operations.
