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"
LVL 2
DaveMonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DaveMonAuthor Commented:
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
0
smoeeCommented:
Put textboxes txtDaystart and txtNightstart, labels lblDaytime, lblNighttime and lblRatio, and button Button1 on a form and add this code:

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 = CType(txtDaystart.Text, DateTime)
      tsNightStart = CType(txtNightstart.Text, DateTime)

      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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

DaveMonAuthor Commented:
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
0
smoeeCommented:
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
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
To make it safer, enclose your DateTime.Parse() calls in a Try...Catch block.  Otherwise, invalid dates in your TextBoxes will cause a fatal crash in your app.  =\
0
DaveMonAuthor Commented:
KABOOM !!!  that  is the solution !!!  I really  appreciate the help with this problem !!!

many  thanks  !!!


Dave
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.