Open In App

DateTime.AddMinutes() Method in C#

Improve
Improve
Like Article
Like
Save
Share
Report

This method is used to return a new DateTime that adds the specified number of minutes to the value of this instance.

Syntax:

public DateTime AddMinutes (double value);

Here, value is a number of whole and fractional minutes. 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 minutes 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.AddMinutes(Double) Method
using System;
  
class GFG {
  
// Main Method
public static void Main()
{
  
    // Creating a DateTime object
    DateTime d1 = new DateTime(2018, 9,
                            7, 7, 0, 0);
  
    // Taking minutes
    double[] m1 = {.01567, .06743, 12.12347,
                           .89, .6666, 250.0};
  
    foreach(double m2 in m1)
    {
  
        // using the method
        Console.WriteLine("{0} + {1} minute(s) = {2}", d1,
                                   m2, d1.AddMinutes(m2));
    }                    
}
}


Output:

09/07/2018 07:00:00 + 0.01567 minute(s) = 09/07/2018 07:00:00
09/07/2018 07:00:00 + 0.06743 minute(s) = 09/07/2018 07:00:04
09/07/2018 07:00:00 + 12.12347 minute(s) = 09/07/2018 07:12:07
09/07/2018 07:00:00 + 0.89 minute(s) = 09/07/2018 07:00:53
09/07/2018 07:00:00 + 0.6666 minute(s) = 09/07/2018 07:00:39
09/07/2018 07:00:00 + 250 minute(s) = 09/07/2018 11:10:00

Example 2:




// C# program to demonstrate the
// DateTime.AddMinutes(Double) Method
using System;
  
class GFG {
  
// Main Method
public static void Main()
{
  
    // Creating a DateTime object
    // by taking it MaxValue
    DateTime d1 = DateTime.MaxValue;
  
    // Taking minute variable
    double m1 = 1.7;
  
    // Using the Method will error as the
    // resulting DateTime is greater than 
    // MaxValue
    Console.WriteLine(d1.AddMinutes(m1));
}
}


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:



Last Updated : 18 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads