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: Add to date

From Wiki

Jump to: navigation, search

Adding a day, a month or a year to a date in VB.Net is very easy but takes some getting used to.

Here is the code.

  1. Module Module1
  2.  
  3.     Sub Main()
  4.         Dim _Date As New Date(2008, 6, 12)
  5.         Console.WriteLine("begin")
  6.         Console.WriteLine(_Date.ToString("dd/MM/yyy"))
  7.         _Date = _Date.AddDays(10)
  8.         Console.WriteLine("added 10 days")
  9.         Console.WriteLine(_Date.ToString("dd/MM/yyy"))
  10.         _Date = _Date.AddMonths(10)
  11.         Console.WriteLine("added 10 months")
  12.         Console.WriteLine(_Date.ToString("dd/MM/yyy"))
  13.         _Date = _Date.AddYears(10)
  14.         Console.WriteLine("added 10 years")
  15.         Console.WriteLine(_Date.ToString("dd/MM/yyy"))
  16.         _Date = _Date.Add(New TimeSpan(10, 0, 0, 0, 0))
  17.         Console.WriteLine("added 10 days with timespan")
  18.         Console.WriteLine(_Date.ToString("dd/MM/yyy"))
  19.         Console.ReadLine()
  20.     End Sub
  21.  
  22. End Module

The americans just have to switch the dd and MM to make it more readable to them.

Just a little warning _date.adddays onitself will not add anything to _date which is a bit strange if you first try this.

If you run the above you will see the following.

Image:Resultdateadding.jpg

538 Rating: 1.3/5 (3 votes cast)