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 Time from a date (hours, minutes, seconds)
From Wiki
It seem a bit strange but to substract you have to add.
look at the code to find out more.
- Module Module1
- Sub Main()
- Dim _Date As New Date(2008, 6, 12, 12, 12, 12)
- Console.WriteLine("begin")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
- _Date = _Date.AddHours(-10)
- Console.WriteLine("subtracting 10 hours")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
- _Date = _Date.AddMinutes(-10)
- Console.WriteLine("subtracting 10 minutes")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
- _Date = _Date.AddSeconds(-10)
- Console.WriteLine("subtracting 10 seconds")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
- _Date = _Date.Add(New TimeSpan(10, 10, 0, 10, 0).Negate)
- Console.WriteLine("subtracting 10 days, 10 hours and 10 seconds with timespan")
- Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
- Console.ReadLine()
- End Sub
- End Module
See how all numbers are negative. And also how you can make a negative timespan just be adding .Negate.
This is the result of the above.




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