I'm subtracting one date from another, it appears to output the result as a TimeSpan, which I can't format with the Format() function.
Any ideas on how to get around this?
Printable View
I'm subtracting one date from another, it appears to output the result as a TimeSpan, which I can't format with the Format() function.
Any ideas on how to get around this?
how are you trying to format it in?? It is a span of time, not a particular "date", so you cant format it into a "date". What are you trying to get?
There are different methods of timespan, like days, hours, minutes, etc...
You also might want to look into the DateDiff function (in the Microsoft.VisualBasic namespace) if you want years, months, etc...Code:'shows total days between the two dates
Dim MyDate As Date = Date.Now
Dim MyFutureDate As Date = Date.Parse("12/25/2007")
Dim Difference As TimeSpan = MyFutureDate.Subtract(MyDate)
MessageBox.Show(Difference.TotalDays & ":TotalDays")
For an example on DateDiff, see post #8 in this thread:
http://www.vbforums.com/showthread.php?t=385300
I'd suggest using the .ToString method rather than Format in general, but neither work for a TimeSpan anyway. Note that the largest unit a TimeSpan will give you is days, because months and years are dependent on the start and end of the period, which the TimeSpan doesn't know about. You can do something like this:VB Code:
Dim d1 As Date Dim d2 As Date Dim t As TimeSpan = d2.Subtract(d1) MessageBox.Show(String.Format("{0}:{1:d2}:{2:d2}:{3:d2}", t.Days, t.Hours, t.Minutes, t.Seconds))