6 Years
Discussion Span
Last Post by ddanbe

Use the overloaded - operator to substract 2 DateTime structures. A TimeSpan object is returned.
There is also a Substract method.


here is a code snippet that shows the number of days to Christmas

DateTime dt1 = DateTime.Now;
            DateTime dt2;
            DateTime.TryParse("12/25/2010", out dt2);
            TimeSpan ts = dt2.Subtract(dt1);
            int daysToChristmas = ts.Days;


Following code calculates the difference in days between two dates and constructs a TimeSpan value for that difference.

DateTime oldDate = new DateTime(2010,9,15);
DateTime newDate = DateTime.Now;

// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;

// Difference in days.
int differenceInDays = ts.Days;

System.Console.WriteLine("Difference in days: {0} ", differenceInDays);
Votes + Comments
U r genius

Just a brief note here in relation to answering threads.

Person A asks question
Person B gives general answer with conceptual method
Person C gives spoon-fed answer complete with code
Person D gives spoon-fed answer complete with code
Person A takes spoon-fed answers, doesn't bother to look into conceptual method or learn how it works and learns nothing

Just a thought is all :twisted: carry on.

Votes + Comments
Good remark.

I totally disagree with your perspective. When I was learning to code, 15 years ago, I really appreciated all the spoon fed answers I received. Conceptual method is good in a classroom or in a forum where thats the topic of discussion. But I think to make it the primary method of helping on these forums raises the bar too high. No one is going to learn anything and the forum looses its value to new coders.

On the other hand, if most of your fellow forum members agree with you, then I'm in the wrong place.


Let me put it in a different perspective then.

Person A gives an example of their work and is stuck in a particular place where their code isn't working. They've tried a few alternatives and it's apparent that they have the right general idea but are making a mistake or just misunderstood how something worked.
Person B gives a working example showing how it corrects the problem Person A encountered and Person A sees the reasoning and the mistake and corrects their work

Person A posts a question asking for a handout of code because they haven't bothered to look into or try to solve the issue themselves. They show no effort in their work and show no understanding of the key concepts in coding.
Person B gives a handout with no serious explanation of the concepts behind it.
Person A takes the handout, uses it, completes their project but comes back a week later with the same question (or a variant) because they didn't learn anything.

Not saying 100% that this falls into either category, just stating the logic behind my earlier statement.


@edepperson: NO, you are not in the wrong place!
But if you like to code and learn, PLEASE show some effort and post some(perhaps rubbish) code. Don't be affraid to do that! Believe me, you will get more response.
I did that and the community here, most of the time, helped me out.
A question like: "Give me the code to calculate PI" will not work.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.