DateTime.AddSeconds() Method in C# - GeeksforGeeks

18 Jan, 2019
This method is used to return a new DateTime that adds the specified number of seconds to the value of this instance.
Syntax:
public DateTime AddSeconds (double value);
Here, value is a number of whole and fractional seconds. The value parameter can be negative or positive.
Return Value: This method returns an object whose value is the sum of the date and time represented by this instance and the number of seconds represented by value.
Exception: This method will throw ArgumentOutOfRangeException if the resulting DateTime is less than MinValue or greater than MaxValue.
Below programs illustrate the use of the above-discussed method:
Example 1:
// C# program to demonstrate the// DateTime.AddSeconds(Double) Methodusing System;  class GFG {  // Main Methodpublic static void Main(){          // defining the format of date    string dateFormat = "MM/dd/yyyy hh:mm:ss";      // Creating a DateTime object and     // taking a particular date and time    DateTime d1 = new DateTime(2018, 9, 7, 7, 0, 0);      Console.WriteLine("Original date: {0}",                  d1.ToString(dateFormat));      // Taking seconds    int sec = 30;      // using method    DateTime d2 = d1.AddSeconds(sec);      Console.WriteLine("After Using Method: {0}",                    d2.ToString(dateFormat));}}
Output:
Original date: 09/07/2018 07:00:00
After Using Method: 09/07/2018 07:00:30

Example 2:
// C# program to demonstrate the// DateTime.AddSeconds(Double) Methodusing System;  class GFG {  // Main Methodpublic static void Main(){          // defining the format of date    string dateFormat = "MM/dd/yyyy hh:mm:ss";      // Creating a DateTime object and     // taking a MaxValue of Date    DateTime d1 = DateTime.MaxValue;      Console.WriteLine("Original date: {0}",                  d1.ToString(dateFormat));      // Taking seconds    int sec = 17;      // using method will give error as the    // resulting DateTime will be greater     // than the MaxValue    DateTime d2 = d1.AddSeconds(sec);      Console.WriteLine("After Using Method: {0}",                      d2.ToString(dateFormat));}}
Runtime Error:
Unhandled Exception:System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime.Parameter name: value
Note:
This method does not change the value of this DateTime. Instead, it returns a new DateTime whose value is the result of this operation.
The fractional part of the value is the fractional part of a minute. For example, 7.5 is equivalent to 7 minutes, 30 seconds, 0 milliseconds, and 0 ticks.
The value parameter is rounded to the nearest millisecond.
Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.addseconds?view=netframework-4.7.2
CSharp DateTime Struct
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 50 C# Interview Questions & Answers
Extension Method in C#
HashSet in C# with Examples
Partial Classes in C#
C# | Inheritance
Convert String to Character Array in C#
Linked List Implementation in C#
C# | How to insert an element in an Array?
C# | List Class
Difference between Hashtable and Dictionary in C#