Link to home
Start Free TrialLog in
Avatar of DaveMon
DaveMonFlag for United States of America

asked on

Time Values in VB.net - 500 points to Help me solve this !!!

Hello Experts,
         This project has been a particular pain to me, I have asked a question here at EE in the past
        using VB6 and got some resovlve, but I think it could go better.... my points offering should reflect this

         now using VB.NET 2003
        ' my project is an environment simulator...which "controls" the amount of daylight in an environment

        ' what I want is for the user to be able to define "daystart" time and "nightstart" times
        ' as regular AM/PM values....... ie. daystart time = 8:35 AM  nightstart = 10:17 PM into textboxes
        ' or DomainUpDownCOntrols.
        ' then based on those values, determine the length of "daytime" and "nighttime"
        ' daytime = time from daystart to nightstart
        ' nighttime = time from nightstart to daystart  
        ' nighttime + daytime should equal 24 hours
        ' my goal is to then express this as a ratio, i.e  12/12, 18/6, 23/1 etc.
        ' and also preferably as 5.43/18.57  representing hours/minutes of the day/night ratio
        ' ( I do not care about seconds )
        ' after I have this part working I will set some boolean vars that specify
        'If "Day" = "True" Then Night = "False"
Avatar of DaveMon
DaveMon
Flag of United States of America image

ASKER

Thanks for any help on this...I have  scoured the net looking for some example code and I  have  found some...but  nothing that really clarifies what Im trying to do......  notice  in my history  that  I asked  almost the same  question  3  years  ago   :(


Dave
ASKER CERTIFIED SOLUTION
Avatar of smoee
smoee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
The magic behind smoees code is the TimeSpan() class.  First you convert your two times to DateTime() instances.  smoee has used CType(), but you could also use DateTime.Parse().

When you subtract two DateTime instances with the DateTime.Subtract() method you get a TimeSpan instance back that represents the amount of time between the two DateTimes.  You can get the WHOLE number of Days, Hours, Minutes, Seconds, Milliseconds and even Ticks that the TimeSpan represents by using the Properties with those same names.  You can also get the TotalHours, TotalMinutes, etc. that it represents.  So if you had a TimeSpan that represented 2 1/2 hours you would get 2.5 from the TotalHours property but 2 and 30 from the Hours and Minutes properties respectively.

See here for more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtimespanclasstopic.asp
Avatar of DaveMon

ASKER

Thanks for this code it does exactly as I have asked...... can  I add one small question reagrding error trapping on this.....  if  someone enters an AM date for the start and a PM  for the finsih....all is  good.  If  you  enter  a PM  ...say  9:45  PM  and   6:15  AM  ..  you  get  a negative  number  ....   is  there  any  easy  way  around  this  so  that it doesnt  give  a negative  value?  If  you prefer  I will ask  another  question ...I just  hate how  noone  will understand  the  ground  we  have  covered in this post  and they  will suggest  millions  of things  that wont  apply  since  they wont  read this post....     thanks  to both  of you   !!!


DaveMon
Avatar of smoee
smoee

To handle this, I checked if the night start was before the day start.  If so, I added a day to the night start value.  I also changed the CType to DateTime.Parse.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim tsDayStart As DateTime
      Dim tsNightStart As DateTime
      Dim sDaytime As String
      Dim sNighttime As String

      tsDayStart = DateTime.Parse(txtDaystart.Text)
      tsNightStart = DateTime.Parse(txtNightstart.Text)

      If tsNightStart < tsDayStart Then
            tsNightStart = tsNightStart.AddDays(1)
      End If

      sDaytime = (Math.Round(tsNightStart.Subtract(tsDayStart).TotalHours, 2)).ToString
      sNighttime = (Math.Round(tsDayStart.AddDays(1).Subtract(tsNightStart).TotalHours, 2)).ToString

      lblDaytime.Text = sDaytime
      lblNighttime.Text = sNighttime
      lblRatio.Text = sDaytime & "/" & sNighttime

End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DaveMon

ASKER

KABOOM !!!  that  is the solution !!!  I really  appreciate the help with this problem !!!

many  thanks  !!!


Dave