?
Solved

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

Posted on 2006-04-18
7
Medium Priority
?
269 Views
Last Modified: 2008-02-01
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"
0
Comment
Question by:DaveMon
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Author Comment

by:DaveMon
ID: 16480803
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
 
LVL 5

Accepted Solution

by:
smoee earned 1600 total points
ID: 16481507
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16482499
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:DaveMon
ID: 16492866
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
 
LVL 5

Expert Comment

by:smoee
ID: 16493618
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 16493897
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
 
LVL 2

Author Comment

by:DaveMon
ID: 16548287
KABOOM !!!  that  is the solution !!!  I really  appreciate the help with this problem !!!

many  thanks  !!!


Dave
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question