Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

VB.Net: Substract Time from a date (hours, minutes, seconds)

From Wiki

Jump to: navigation, search

It seem a bit strange but to substract you have to add.

look at the code to find out more.

  1. Module Module1
  2.  
  3.     Sub Main()
  4.         Dim _Date As New Date(2008, 6, 12, 12, 12, 12)
  5.         Console.WriteLine("begin")
  6.         Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
  7.         _Date = _Date.AddHours(-10)
  8.         Console.WriteLine("subtracting 10 hours")
  9.         Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
  10.         _Date = _Date.AddMinutes(-10)
  11.         Console.WriteLine("subtracting 10 minutes")
  12.         Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
  13.         _Date = _Date.AddSeconds(-10)
  14.         Console.WriteLine("subtracting 10 seconds")
  15.         Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
  16.         _Date = _Date.Add(New TimeSpan(10, 10, 0, 10, 0).Negate)
  17.         Console.WriteLine("subtracting 10 days, 10 hours and 10 seconds with timespan")
  18.         Console.WriteLine(_Date.ToString("dd/MM/yyyy HH/mm/ss"))
  19.         Console.ReadLine()
  20.     End Sub
  21.  
  22. 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.

Image:Resulttimesubtracting.jpg

542 Rating: 1.8/5 (4 votes cast)