Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    57
    Thanks
    11
    Thanked 0 Times in 0 Posts

    [VB NET] - Calculating days into weeks

    I'm a Gardener and i enjoy cross breeding different vegetables in the summer. I Made this program last year that helps me keep track of things. Well i need to add to this program a piece of code that will let me enter the date the plant was started from seed and by each passing day when i start the program it will tell me how long the seed has been in the ground all the way up to harvest..

    I Have a textbox that has the date in it and a label that says "Day: 1" and another Label that says "Week: 0". Each passing day it will add another day to the day label so it reads Day: 2, Day: 3, Day 4 and so forth.. When the label reaches "Day: 7" it will add a week into the Week Label so it reads "Week 1, Week 2, Week 3 and so forth..."

    Can some please help me out with this? This is something i been wanting to add this to my custom program for so long, but i just don't have the slightest clue on how to accomplish this.

    I'm been working on a my own strain of tomatoes for several years trying to develop a super fast variety of tomato and i have to keep track of hundreds of tomato plants every year.

  • #2
    New Coder
    Join Date
    Aug 2013
    Posts
    36
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Hi kor81,

    you can use the DateTime structure to save the date in a variable like this:

    Code:
    Dim seedingDate As DateTime = DateTime.Parse("5/12/2013")
    Here is more information about converting a String to a DateTime: http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

    To obtain a timespan you just subtract a DateTime from another:

    Code:
    Dim seedingDate As DateTime = DateTime.Parse("5/12/2013")
    Dim now As DateTime = DateTime.Now
    Dim elapsedTime As TimeSpan = now.Subtract(seedingDate)
    You can now convert the TimeSpan to a String as follows:

    Code:
    Dim seedingDate As DateTime = DateTime.Parse("7/11/2013")
    Dim now As DateTime = DateTime.Now
    Dim elapsedTime As TimeSpan = now.Subtract(seedingDate)
    
    Dim weeks As Integer = Convert.ToInt32(elapsedTime.Days / 7)
    Dim days As Integer = elapsedTime.Days - (weeks * 7)
    
    Dim format As String = "{0} week(s) and {1} day(s)"
    Dim formattedElapsedTime As String = String.Format(format, weeks, days)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •