Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime AddMinutes method not working

Tags:

c#

.net

datetime

The purpose of my method is to get the currentTime, and set it back for 20 minutes. For what I can see, my method is correct, but the output shows something else.

This i my code:

DateTime currentTime = DateTime.Now;
double minuts = -20;
currentTime.AddMinutes(minuts);

Console.WriteLine("Nuværende tid: "+currentTime);

The output are as followed:

25-11-2011 14:01:54

My result should be:

25-11-2011 13:41:54.

Thanks!

like image 833
Anders Jensen Avatar asked Nov 25 '11 13:11

Anders Jensen


People also ask

How to use AddMinutes 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.

How to add minutes in DateTime now in c#?

AddMinutes() Method in C# 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);

How do I subtract hours and minutes in C#?

Add(new TimeSpan(-2,0,0)); DateTime original = new DateTime(year, month, day, 17, 30, 0); DateTime updated = original. Add(new TimeSpan(0,-45,0)); Or you can also use the DateTime. Subtract(TimeSpan) method analogously.


2 Answers

Description

The AddMinutes function returns a DateTime.

DateTime.AddMinutes Method Returns a new DateTime that adds the specified number of minutes to the value of this instance.

Sample

DateTime currentTime = DateTime.Now;
double minuts = -20;
currentTime = currentTime.AddMinutes(minuts);

Console.WriteLine("Nuværende tid: "+currentTime);

More Information

  • DateTime.AddMinutes Method
like image 92
dknaack Avatar answered Oct 11 '22 10:10

dknaack


try:

currentTime = currentTime.AddMinutes(minuts);
like image 40
doblak Avatar answered Oct 11 '22 11:10

doblak