What is the easiest way to subtract time in C#?
These can all be done with DateTime.Add(TimeSpan)
since it supports positive and negative timespans.
DateTime original = new DateTime(year, month, day, 8, 0, 0);
DateTime updated = original.Add(new TimeSpan(5,0,0));
DateTime original = new DateTime(year, month, day, 17, 0, 0);
DateTime updated = original.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.
How can I subtract 6 hour from the current time?
You can use AddHours, it will subtract hours if negative number is passed.
DateTime dt1 = dt.AddHours(-6);
C# subtract time (hours minutes)
you could do something like this
//Datetime(Year,month,day,hour,min,sec)
DateTime date1 = new DateTime(2012, 1, 1, 20, 0, 0);
DateTime date2 = new DateTime(2012, 1, 2, 1, 0, 0);
string minutes = (date2.Subtract(date1).TotalMinutes).ToString();
Tested and works 300 minutes (5 hours)
C# time subtraction Issue
It seems like you're showing a lot of code that's difficult to reproduce for us, and the variable names are not the clearest. I'm assuming dt
stands for "datetime", and edt
stands for "end datetime". If that's correct, then you're subtracting the end date from the start date instead of the other way around (you should subtract the smaller from the larger).
So, here's how to get the difference between start and end (I'm using Hindi culture info for this):
var dateFormatCulture = new CultureInfo("hi-IN");
var startDate = DateTime.Parse("4/5/2017 11:56:27 PM", dateFormatCulture);
var endDate = DateTime.Parse("5/5/2017 12:10:27 AM", dateFormatCulture);
var difference = endDate.Subtract(startDate);
You say you want "the exact time like 14 minutes". I'm not sure if that means you don't want to show the rest of the values, but here are a few ways you can display it.
Console.WriteLine($"General short string format: {difference:g}");
Console.WriteLine(
"Custom string format: {0} days, {1} hours, {2} minutes, {3} seconds",
difference.Days, difference.Hours, difference.Minutes, difference.Seconds);
Console.WriteLine("In terms of minutes, the total minutes difference is: {0}",
difference.TotalMinutes);
Notice that there's a difference between the second an third example in the methods being called to show the minutes . Minutes
will display just the minutes portion of the difference. TotalMinutes
will display the entire difference in terms of minutes. In your case they are the same, but if the difference was 1 hour and 14 minutes, Minutes
would still be 14
, but TotalMinutes
would be 74
.
The output looks like:
How to subtract a datetime from another datetime?
In .NET, if you subtract one DateTime
object from another, you will get a TimeSpan
object. You can then use the Ticks
property on that TimeSpan
object to get the number of ticks between the two DateTime
objects. However, the ticks will be represented by a Long
, not a Double
.
DateTime date1;
DateTime date2;
Long diffTicks = (date2 - date1).Ticks;
There are other interesting properties on the TimeSpan
object like TotalMilliseconds
and TotalMinutes
and things like that which can help you out, and may be more what you are looking for.
Subtract days from a DateTime
That error usually occurs when you try to subtract an interval from DateTime.MinValue
or you want to add something to DateTime.MaxValue
(or you try to instantiate a date outside this min-max interval). Are you sure you're not assigning MinValue
somewhere?
Subtracting Time from a datetime
var name = DateTime.Now.AddHours(-2)
.ToString("XX-yyyyMMdd-hhmm", CultureInfo.InvariantCulture);
This will give you the date/time in your requested format, but obviously this will change every minute.
Related Topics
Windows 7 and Vista Uac - Programmatically Requesting Elevation in C#
Sqlbulkcopy Insert with Identity Column
What Are Some Reasons Networkstream.Read Would Hang/Block
I Didn't Find "Zipfile" Class in the "System.Io.Compression" Namespace
Inconsistency in Divide-By-Zero Behavior Between Different Value Types
No Templates in Visual Studio 2017
SQL Where Clause Matching Values with Trailing Spaces
Open Default Mail Client Along with a Attachment
What Is the "Right" Way to Bring a Windows Forms Application to the Foreground
Use Reflection to Invoke an Overridden Base Method
What's the Best Strategy for Equals and Gethashcode
How to Stop Flickering C# Winforms
How to Convert Datetime To/From Specific String Format (Both Ways, E.G. Given Format Is "Yyyymmdd")