Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 14th, 2011, 09:04 PM
Registered User
 
Join Date: May 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trouble with calculations

I am Trying to calculate two times to find out how many hours are between them.
When the user clicks the Log in button it gets the time they logged in and then when they click the Log out button they get the time they logged out
and inputs both times in seperate textboxs. If anyone can help with this I would appreciate it

Thank you Jeremy
 
Old May 15th, 2011, 08:49 AM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

There is a whole object used for this, called the TimeSpan object.

Code:
Dim startTime As TimeSpan
Dim stopTime As TimeSpan
Dim duartion As TimeSpan
duration = startTime.Subtract(stopTime)
that'll sort you out for a while.
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old May 15th, 2011, 09:16 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Here is a hint

http://dotnetdud.blogspot.com/2011/0...-two-time.html

You can assign the value of textbox to the variable and modify it

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 16th, 2011, 08:23 AM
Authorized User
Points: 319, Level: 6
Points: 319, Level: 6 Points: 319, Level: 6 Points: 319, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2010
Location: MIssouri
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default dATE aND tIME

here is code for 2 different ways of doing the same.
of course timespan is a lot shorter method!

you need to dim StartTime And StopTime AS DateTime Rather than TimeSpan Though
Code:
    Dim LBL As New Label
    Dim Btn As New Button
    Dim startTime As DateTime

    Dim stopTime As DateTime

    Dim duration As TimeSpan


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Btn = New Button
        Btn.Text = "Start Time"
        Btn.Name = "BtnStart"
        Btn.Location = New Point(10, 10)
        Btn.Size = New Size(40, 40)
        Me.Controls.Add(Btn)

        AddHandler Btn.Click, New System.EventHandler(AddressOf Btn_Click)

        Btn = New Button
        Btn.Text = "End Time"
        Btn.Name = "BtnEnd"
        Btn.Location = New Point(120, 10)
        Btn.Size = New Size(40, 40)
        Me.Controls.Add(Btn)

        AddHandler Btn.Click, New System.EventHandler(AddressOf Btn_Click)

        LBL = New Label
        LBL.Name = "lblStart"
        LBL.Location = New Point(10, 60)
        LBL.Text = Now
        Me.Controls.Add(LBL)

        LBL = New Label
        LBL.Name = "lblEnd"
        LBL.Location = New Point(120, 60)
        Me.Controls.Add(LBL)

        LBL = New Label
        LBL.Name = "lblEnd"
        LBL.Location = New Point(110, 100)
        Me.Controls.Add(LBL)

        LBL = New Label
        LBL.Name = "lblEnd"
        LBL.Location = New Point(110, 140)
        Me.Controls.Add(LBL)

        LBL = New Label
        LBL.Name = "lblEnd"
        LBL.Location = New Point(110, 180)
        Me.Controls.Add(LBL)

        LBL = New Label
        LBL.Name = "lblEnd"
        LBL.Location = New Point(110, 220)
        Me.Controls.Add(LBL)


    End Sub

    Private Sub Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If (CType(sender, Button).Name()) = "BtnStart" Then
            Me.Controls(2).Text = Now

        Else

            Me.Controls(3).Text = Now
            Me.Controls(4).Text = DateDiff(DateInterval.Hour, CDate(Me.Controls(2).Text), CDate(Me.Controls(3).Text))
            Me.Controls(5).Text = " Hours:" & DateDiff(DateInterval.Hour, CDate(Me.Controls(2).Text), CDate(Me.Controls(3).Text))
            Me.Controls(6).Text = " Minutes:" & DateDiff(DateInterval.Minute, CDate(Me.Controls(2).Text), CDate(Me.Controls(3).Text))
            Me.Controls(7).Text = " Seconds:" & DateDiff(DateInterval.Second, CDate(Me.Controls(2).Text), CDate(Me.Controls(3).Text))
            startTime = Me.Controls(2).Text


            stopTime = Me.Controls(3).Text
            duration = startTime.Subtract(stopTime)
            MsgBox(duration.ToString)

        End If
    End Sub
Just copy And Pase Between The Class Declaration And THe End Class




Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculations with a string mgeorge BOOK: Beginning ASP 3.0 4 October 10th, 2008 01:45 PM
Positioning Calculations rit01 ASP.NET 1.x and 2.0 Application Design 3 February 22nd, 2006 03:10 PM
time calculations lizhaskin Access 1 November 10th, 2005 08:48 AM
Date Calculations Stu_morris40 Infopath 0 November 1st, 2005 11:53 AM
Date Calculations mvollmer Classic ASP Databases 4 October 15th, 2003 10:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.