DateTime.AddMinutes() Method in C#



The DateTime.AddMinutes() method in C# is used to add the specified number of minutes to the value of this instance. It returns the new DateTime.

Syntax

Following is the syntax −

public DateTime AddMinutes (double m);

Above, m are the minutes to be added. If a negative value is added, then the minutes will get subtracted.

Example

Let us now see an example to implement the DateTime.AddMinutes() method −

using System;
public class Demo {
   public static void Main(){
      DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25);
      DateTime d2 = d1.AddMinutes(25);
      System.Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
      System.Console.WriteLine("
New DateTime (After adding minutes) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2);    } }

Output

This will produce the following output −

Initial DateTime = 10 September 2019, 05:15:25
New DateTime (After adding minutes) = 10 September 2019, 05:40:25

Example

Let us now see another example to implement the DateTime.AddMinutes() method −

using System;
public class Demo {
   public static void Main(){
      DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25);
      DateTime d2 = d1.AddMinutes(-15);
      System.Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
      System.Console.WriteLine("
New DateTime (After subtracting minutes) = {0:dd} {0:y}, {0:hh}:   {0:mm}:{0:ss} ", d2);    } }

Output

This will produce the following output −

Initial DateTime = 10 September 2019, 05:15:25
New DateTime (After subtracting minutes) = 10 September 2019, 05:00:25
Updated on: 2019-11-11T05:43:56+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements