adding time values from two textbox

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 4888 Views - Last Post: 11 October 2012 - 11:48 AM Rate Topic: -----

#1 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

adding time values from two textbox

Posted 11 October 2012 - 03:01 AM

i have two textbox..TimeInTextbox that contains time in for example(8:00 am) and TimeOutTextbox that contains time out for example(5:00 pm), i want to add TimeInTextbox and TimeOutTextbox to get the total working hours but how am i suppposed to do that?
Is This A Good Question/Topic? 0
  • +

Replies To: adding time values from two textbox

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: adding time values from two textbox

Posted 11 October 2012 - 03:09 AM

Use TimeSpan. You have examples on provided link, just scroll down to Remarks section. 3rd should do the trick for you.
Was This Post Helpful? 0
  • +
  • -

#3 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

Re: adding time values from two textbox

Posted 11 October 2012 - 03:27 AM

View Postlucky3, on 11 October 2012 - 03:09 AM, said:

Use TimeSpan. You have examples on provided link, just scroll down to Remarks section. 3rd should do the trick for you.


but i think it doesn't work with textbox
Was This Post Helpful? 0
  • +
  • -

#4 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 09-October 12

Re: adding time values from two textbox

Posted 11 October 2012 - 06:32 AM

Solved the problem. Hope it works for you.
    Public Shared Function CalculateWorkingHours(ByVal StartTime As String, ByVal EndTime As String) As String
        Dim Result As String = ""
        Dim StartSplit As String()
        Dim EndSplit As String()
        Dim StartHour As Integer
        Dim StartMin As Integer
        Dim EndHour As Integer
        Dim EndMin As Integer
        Dim WorkingHour As String
        Dim WorkingMin As String
        If StartTime.Contains(" am") = True Then
            StartSplit = StartTime.Replace(" am", "").Split(":")
            StartHour = CInt(StartSplit(0))
            StartMin = CInt(StartSplit(1))
        ElseIf StartTime.Contains(" pm") = True Then
            StartSplit = StartTime.Replace(" pm", "").Split(":")
            StartHour = CInt(StartSplit(0) + 12)
            StartMin = CInt(StartSplit(1))
        End If
        If EndTime.Contains(" am") = True Then
            EndSplit = EndTime.Replace(" am", "").Split(":")
            EndHour = CInt(EndSplit(0))
            EndMin = CInt(EndSplit(1))
        ElseIf EndTime.Contains(" pm") = True Then
            EndSplit = EndTime.Replace(" pm", "").Split(":")
            EndHour = CInt(EndSplit(0) + 12)
            EndMin = CInt(EndSplit(1))
        End If
        WorkingHour = EndHour - StartHour
        WorkingMin = EndMin - StartMin
        If WorkingHour.Length = 1 Then
            WorkingHour = "0" & WorkingHour
        End If
        If WorkingMin.Length = 1 Then
            WorkingMin = "0" & WorkingMin
        End If
        Result = WorkingHour & ":" & WorkingMin
        Return Result
    End Function


Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: adding time values from two textbox

Posted 11 October 2012 - 07:01 AM

View Postjhedonghae, on 11 October 2012 - 03:01 AM, said:

i have two textbox..TimeInTextbox that contains time in for example(8:00 am) and TimeOutTextbox that contains time out for example(5:00 pm), i want to add TimeInTextbox and TimeOutTextbox to get the total working hours but how am i suppposed to do that?


My first question would be "How did the times get into the TextBoxes?" If you used Now() to generate the text to go into your TextBoxes, why not change it to place the Now() values into DateTime variables. It then becomes easy to do a TimeSpan calculation using those variables. You can use Format to place the text into the TextBoxes.

Alternatively, you can create the DateTime variables on the fly and convert the TextBox text into them, then use the TimeSpan.
Was This Post Helpful? 0
  • +
  • -

#6 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

Re: adding time values from two textbox

Posted 11 October 2012 - 08:01 AM

View Postlar3ry, on 11 October 2012 - 07:01 AM, said:

View Postjhedonghae, on 11 October 2012 - 03:01 AM, said:

i have two textbox..TimeInTextbox that contains time in for example(8:00 am) and TimeOutTextbox that contains time out for example(5:00 pm), i want to add TimeInTextbox and TimeOutTextbox to get the total working hours but how am i suppposed to do that?


My first question would be "How did the times get into the TextBoxes?" If you used Now() to generate the text to go into your TextBoxes, why not change it to place the Now() values into DateTime variables. It then becomes easy to do a TimeSpan calculation using those variables. You can use Format to place the text into the TextBoxes.

Alternatively, you can create the DateTime variables on the fly and convert the TextBox text into them, then use the TimeSpan.


when the user clicks the log in button the date will be added to the datetextbox and timein will be added to the timeintextbox, and when log out is click timeout will be added to the timeouttextbox.. i had attach an image so that you can understand me more..
now what i want is to fill the totalhourswork by adding the timeintextbox and timeouttextbox and subtract 1 hour for the break time
and for the overtime by subtracting the timeintextbox and timeouttextbox and subtract 1 hour for the break time
hope you understand me..please help!

View Postlar3ry, on 11 October 2012 - 07:01 AM, said:

View Postjhedonghae, on 11 October 2012 - 03:01 AM, said:

i have two textbox..TimeInTextbox that contains time in for example(8:00 am) and TimeOutTextbox that contains time out for example(5:00 pm), i want to add TimeInTextbox and TimeOutTextbox to get the total working hours but how am i suppposed to do that?


My first question would be "How did the times get into the TextBoxes?" If you used Now() to generate the text to go into your TextBoxes, why not change it to place the Now() values into DateTime variables. It then becomes easy to do a TimeSpan calculation using those variables. You can use Format to place the text into the TextBoxes.

Alternatively, you can create the DateTime variables on the fly and convert the TextBox text into them, then use the TimeSpan.

when the user clicks the log in button the date will be added to the datetextbox and timein will be added to the timeintextbox, and when log out is click timeout will be added to the timeouttextbox.. i had attach an image so that you can understand me more..
now what i want is to fill the totalhourswork by adding the timeintextbox and timeouttextbox and subtract 1 hour for the break time
and for the overtime by subtracting the timeintextbox and timeouttextbox and subtract 1 hour for the break time
hope you understand me..please help!

Attached image(s)

  • Attached Image
  • Attached Image

This post has been edited by jhedonghae: 11 October 2012 - 08:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: adding time values from two textbox

Posted 11 October 2012 - 08:17 AM

You are dealing with a textbox are you? but the you are dealing with the contents of the textbox, which is text. Text is represented in .net with the type String. But you want an object type that represents Date/Time. So
research how you can convert or parse a string into a DateTime object. Remembering to think are all of possible values a string can have, are all of them convertable into time? If not how are you going to handle that?
Was This Post Helpful? 0
  • +
  • -

#8 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

Re: adding time values from two textbox

Posted 11 October 2012 - 08:26 AM

View PostAdamSpeight2008, on 11 October 2012 - 08:17 AM, said:

You are dealing with a textbox are you? but the you are dealing with the contents of the textbox, which is text. Text is represented in .net with the type String. But you want an object type that represents Date/Time. So
research how you can convert or parse a string into a DateTime object. Remembering to think are all of possible values a string can have, are all of them convertable into time? If not how are you going to handle that?


can you suggest me any idea to solve my problem? should i not use textbox? what should i use instead?
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: adding time values from two textbox

Posted 11 October 2012 - 08:35 AM

You need to re read what I wrote and understand and comprehend what it says.

Stop thinking in terms of Control, there is more to programming than just controls.

Research, Experiment and make mistakes.

This post has been edited by AdamSpeight2008: 11 October 2012 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: adding time values from two textbox

Posted 11 October 2012 - 08:44 AM

AdamSpeight2008 gave a really good clue when he mentioned DateTime
Was This Post Helpful? 0
  • +
  • -

#11 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

Re: adding time values from two textbox

Posted 11 October 2012 - 09:25 AM

one question, does is it possible to get the total hours work by adding timeintextbox and timeouttextbox?
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: adding time values from two textbox

Posted 11 October 2012 - 09:38 AM

Yes, but you need to convert the textboxes information (.Text property) to a DateTime structure which is what AdamSpeight2008 pointed out.
Was This Post Helpful? 0
  • +
  • -

#13 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

Re: adding time values from two textbox

Posted 11 October 2012 - 09:42 AM

how can i do that? i'm sorry but i really don't have any idea because i'm not good in vb.net
please help me
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: adding time values from two textbox

Posted 11 October 2012 - 09:42 AM

It also more clues and hints. So re-read, if you don't understand the meaning of a word. Look it up (research) in a Dictionary.
Was This Post Helpful? 0
  • +
  • -

#15 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: adding time values from two textbox

Posted 11 October 2012 - 09:54 AM

View Postjhedonghae, on 11 October 2012 - 09:25 AM, said:

one question, does is it possible to get the total hours work by adding timeintextbox and timeouttextbox?


1. You don't want to add the times. 2 o'clock + 4 o'clock is meaningless.
You want to find the difference between the two times. That's best handled with a TimeSpan.
A TimeSpan works using two variables of type DateTime.

2. A TextBox is a control. You cannot calculate a difference between two controls.

3. The .Text property of TextBox contains a string.
You cannot add or subtract with a string.
You must convert a string to a number to use it in calculations.
The best form of a number to use for calculations is a DateTime variable.

I can assure you that AdamSpeight2008, CharlieMay, lucky3 and myself, have all fully understood what you are trying to do. It's just that you are insisting on trying to do something with a control that just can't be done without doing some conversions or changing the way you store the Time In and Time Out.

Please re-read what we have said. Look up the link you were given. Everything is there to point you in the right direction. Don't be afraid to try stuff. You won't break your computer with code.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2