Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
VB.Net: Substract from a date (days, months, years)
From Wiki
To substract you have to add
- Module Module1
- Sub Main()
- Dim _Date As New Date(2008, 6, 12)
- Console.WriteLine("begin")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy"))
- _Date = _Date.AddDays(-10)
- Console.WriteLine("subtracting 10 days")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy"))
- _Date = _Date.AddMonths(-10)
- Console.WriteLine("subtracting 10 months")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy"))
- _Date = _Date.AddYears(-10)
- Console.WriteLine("subtracting 10 years")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy"))
- _Date = _Date.Add(New TimeSpan(10, 0, 0, 0, 0).Negate)
- Console.WriteLine("subtracting 10 days with timespan")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy"))
- Console.ReadLine()
- End Sub
- End Module
Look how all the numbers are negative and how you can set a timespan to be negative by calling the negate method.
Here is the result.




LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.