i want to subtract previous date from current date. previous date may be 2 months and 15 days or 1 year 9 month and 10 days... like this etc... So please how can i write the Coding in C#. thanks a lot.
-
I've added an answer to this question instead of the duplicate, basically because the world has moved on. Also, this question *specifies* that year/month/day is required, where the other one doesn't. – Jon Skeet Feb 08 '13 at 09:16
-
@JonSkeet I'm generally not a big fan of the fast "close as duplicate" reflex, but this specific question has been asked so many times... IMO the OP doesn't deserve someone spend time to write an answer, as he obviously didn't spent time to search before asking. I just googled "stackoverflow c# subtract dates" and found dozens of identical questions. – ken2k Feb 08 '13 at 09:23
-
@ken2k: Identical, or merely similar? The *exact* nature of what's required can radically change the result. (And as I say, the potential answer has changed. I could have added my answer to the old question, of course, but that feels a little odd as the question isn't *quite* identical enough for it to be the natural answer to that question.) – Jon Skeet Feb 08 '13 at 09:24
-
@JonSkeet The exact nature of this question if hard to define, as the OP didn't explained it well. For the basic `TimeSpan` result, there are tons of identical answers. For the less obvious result of number of months/years...etc., there are also identical answers (http://stackoverflow.com/questions/4638993/difference-in-months-between-two-dates for instance). – ken2k Feb 08 '13 at 09:36
-
@ken2k: As you say, the question is unclear - but that later one looks like a closer duplicate than the one suggested earlier. – Jon Skeet Feb 08 '13 at 09:45
6 Answers
Any answer using TimeSpan
will not be able to give you "2 months and 15 days" - as the length of a month changes over time.
Doing this using the base class libraries is a pain... which is one of the reasons I started the Noda Time project. Amongst its other features, it allows you to determine the Period
between to dates (or dates and times, etc).
For example, let's see how long I've been on Stack Overflow:
LocalDate today = new LocalDate(2013, 2, 8);
LocalDate start = new LocalDate(2008, 9, 26);
// This defaults to using year, month, day units. You can specify alternatives.
Period period = Period.Between(start, today);
Console.WriteLine("{0} years; {1} months; {2} days",
period.Years, period.Months, period.Days);
Output:
4 years; 4 months; 13 days
Or if you actually wanted to subtract a period from a date (the question isn't very clear) you can do that too:
Period period = new PeriodBuilder { Years = 4, Months = 4, Days = 13 }.Build();
LocalDate today = new LocalDate(2013, 2, 8);
LocalDate start = today - period;
Console.WriteLine(start);
Output:
25 September 2008
Note that this doesn't give September 26th, because of the somewhat crazy nature of date/time arithmetic. If you added the period to September 26th you'd get today... but that's not the same thing. Treat this as a warning that you need to be really careful about describing what you want to achieve :)
This second side you can do with the BCL fairly easily though:
DateTime today = new DateTime(2013, 2, 8);
DateTime start = today.PlusYears(-4).PlusMonths(-4).PlusDays(-13);
There's no BCL type to represent that "years, months, days" value though.

- 1,421,763
- 867
- 9,128
- 9,194
-
Forgive my ignorance, but could you perhaps please explain what you mean by `as the length of a month changes over time` – gideon Feb 08 '13 at 10:30
Your question is a little confusing. Do you want to subtract one date from another date, or do you want to subtract a period of time from a date.
1. Subtract one date from another date:
DateTime previousDate = new DateTime(1990, 12, 12);
DateTime currentDate = DateTime.UtcNow;
TimeSpan difference = currentDate - previousDate;
You can then use the TimeSpan
methods to get the difference in various units of time as you like.
Here's more info on TimeSpan: http://msdn.microsoft.com/en-us/library/system.timespan.aspx
2. Subtract a period of time from a date
DateTime currentDate = DateTime.UtcNow;
TimeSpan periodOfTime = new TimeSpan(12, 12, 0, 0);
DateTime newDate = currentDate - periodOfTime;
However, you'll have to calculate yourself what the length of a month is, if that's what you want.

- 18,736
- 7
- 61
- 88
-
I think saying "you'll have to calculate yourself what the length of a month is" is a bit misleading - because it suggests there *is* such a concept. In reality, the length of the month depends on what you're adding it to (or subtracting it from). That's why `TimeSpan` is fundamentally ill-equipped for this job. – Jon Skeet Feb 08 '13 at 09:22
You can use DateTime.Subtract
.
Examples from article:
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);
// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);
// date4 gets 4/9/1996 5:55:00 PM.
System.DateTime date4 = date3.Subtract(diff1);
// diff2 gets 55 days 4 hours and 20 minutes.
System.TimeSpan diff2 = date2 - date3;
// date5 gets 4/9/1996 5:55:00 PM.
System.DateTime date5 = date1 - diff2;

- 47,437
- 25
- 129
- 188
TimeSpan timeSpan = new TimeSpan(2,2,0);
DateTime dateTime = DateTime.Now.Subtract(timeSpan);

- 14,643
- 4
- 38
- 54
When you subtract two date in C# you get a TimeSpan object. You can acces different properties of it to get the actual days, hours, minutes etc. taht it represents:
DateTime a;
DateTime b;
//assign some values
TimeSpan span = a.Subtract(b);
Console.WriteLine("Days: " + span.Days);

- 91,536
- 11
- 82
- 95
The following should do.
TimeSpan diff = DateTime.Now - previousDate;

- 870
- 7
- 14
-
-
Don't have exact idea about that. But if someone consider a month to have exactly 30 days, he could approximate. Anyways referring Noda Time project. :) – Apurv Gupta Feb 08 '13 at 09:22