views:

37

answers:

3

I have a date in the future e.g. 13/10/2008 I need to subtract the current date (today is the 28/09/2010) minus 7 days, so thats 21/09/2010 minus 13/10/2008, which would equal erm, 720 something ?

But the current date won't always be 28/09/2010, obviously.

I need the code for this.

EDIT: When i said future I mean past :)

+2  A: 
Sub Main()
    Dim dt As DateTime = New DateTime(2008, 10, 13)
    ' be careful what you are subtracting from what
    ' the date you have is not in the future (year 2008)
    ' if the date is in the future: (dt.Subtract(DateTime.Now.AddDays(-7))).TotalDays
    ' or simply take the absolute value
    Dim days As Double = (DateTime.Now.AddDays(-7).Subtract(dt)).TotalDays
    Console.WriteLine(days)
End Sub

You will also notice that the TotalDays property is of type Double.

Darin Dimitrov
Error 1 Operator '-' is not defined for types 'Date' and 'Object'.
Jules
Right, sorry, I didn't see you was using VS2005 which doesn't support variable type inference. Please see my update. Types need to be specified when declaring the variables.
Darin Dimitrov
Still getting the same error with your edited code Error 1 Operator '-' is not defined for types 'Date' and 'Object'.
Jules
That's very strange. How about: `(DateTime.Now.AddDays(-7).Subtract(dt)).TotalDays`?
Darin Dimitrov
+1  A: 

13/10/2008 is not exactly in the future :)

Sorry for using C# code, but:

(dateInFuture - DateTime.Now.AddDays(-7)).TotalDays

Should work. Of course the other way around if you mean in the past:

(DateTime.Now.AddDays(-7) - dateInPast).TotalDays
veggerby
Hmmm, having trouble converting that to vb http://www.developerfusion.com/tools/convert/csharp-to-vb/
Jules
A: 

"I need the code for this" seems a bit too much like "Plz give meh teh codez", and your "date in the future" seems a little bit in the past.

Anyway, you should investigate the relevant methods of the DateTime structure, in particular the Subtract method (both overloads, or in alternative its subtraction operator), and you should have a look at the TimeSpan structure too.

You could create a DateTime for the date of today, subtract a TimeSpan of 7 days to it, and then subtract such result to a DateTime representing your date in the future (or, if it is in the past, do the opposite). You'll get a TimeSpan representing the difference in time between the two dates, from which you can easily get the number of days using its Days property.

As other said, to do the first subtraction you can also use the AddDays method of the DateTime structure.

Matteo Italia
There would be no need for sites this like at all if all answers to questions were google it and find out, lol
Jules
Learning to program is learning to put the bricks together. Providing a ready-made answer makes sense for complicated problems, but when all the problem is "find me a call in the library that does what I want" the best answer for me is to gently point to the documentation. Notice that I didn't say "just google it", I provided you some pointers to the relevant sections of the MSDN, so in future you'll be able to sort out such kind of small problems by yourself. And keep in mind that I would have spent less time just providing you teh codez, but that would be spoon-feeding you - useless at most.
Matteo Italia