calculate time

mirzao

Active member
Joined
Nov 10, 2005
Messages
44
Location
Malaysia
Programming Experience
Beginner
Hi,

I 'm using a timer and it gives a format of hr:min:sec displayed in label.text. Using the time displayed, I need to do some calculation . How can i do that?
 
Look up 'Timespan' for your time calculations.
Here is an example of several basic date/time functions.[SIZE=-1]

This serves no function, it is for demonstration of time calculations.
[/SIZE]
VB.NET:
        Dim dt As DateTime
        Dim dtUtc As DateTime
        dt = DateTime.Now
        dtUtc = DateTime.UtcNow
        Dim ts As TimeSpan = dtUtc.Subtract(dt)
        dt = dt.Add(ts)
        dt = dt.AddDays(2)
        dt = dt.AddSeconds(34)
[SIZE=-1]
[/SIZE]
 
Thanks for your time. What i mean is if t >=60sec then add minute. For example

For i=0 to 3
t=t+22 sec
if t>=60 then
'add min
elseif t>=3600 then
'add hour
end if
dt=hour:min:sec
Next
Many thanks
 
What you could do is to create a variable to hold the datetime when the timer is first started, e.g. variable = Now, and everytime you need the time, subtract the variable from Now, to get a timespan representing the time so far. To convert the timespan into a string, use Now.Subtract(variable).ToString() and split the string by the "." to remove the fraction of seconds.
 
Do you just want to add the total elapsed time to some time?

'Put at the top of your form
Public StartTime As DateTime
Public EndTime As DateTime
Public NewTime As DateTime


'Move start and end time line to where they start and end.
StartTime = DateTime.Now
EndTime = DateTime.Now


'This will add the elapsed time to any DateTime value 'NewTime'
Dim ts As TimeSpan = EndTime.Subtract(StartTime)
NewTime = NewTime.Add(ts)

'Takes care of adding hours,minutes and seconds.
TextBox1.text = NewTime.ToShortTimeString

If that is not what you mean by 'I need to do some calculation'
Timespan values can also be accessed as integers to use in calculations also:
Dim eSeconds As Integer = ts.Seconds
Dim eMinutes As Integer = ts.Minutes
Dim eHours As Integer = ts.Hours
NewTime = StartTime.AddMinutes(eMinutes)

Hope that helps, If you still need answers post the code you have (just the bits dealing with the calculations and timer, not the whole form) and be really specific as to what you want to do.
 
I am having a similar problem in a timekeeping application.

The time values are declared as:

dim
as DateTime
dim dtOut as DateTime
dim ts as TimeSpan

Times are entered in textboxes by the user andconverted to the DateTime variables

This code:
ts=dtOut.subtract(dtIn)

Results in ts = -3:00:00 when the user enters:
08:00 AM for the tme In and
08:00 PM for the time out

If the user enters (for example 8:00 AM in and 11:00 AM out ) the calculation is correct. How can I dalculatethe Timespan for a period of time statrting in the AM and ending in the PM?
 
I am having a similar problem in a timekeeping application.

The time values are declared as:

dim
as DateTime
dim dtOut as DateTime
dim ts as TimeSpan

Times are entered in textboxes by the user andconverted to the DateTime variables

This code:
ts=dtOut.subtract(dtIn)

Results in ts = -3:00:00 when the user enters:
08:00 AM for the tme In and
08:00 PM for the time out

If the user enters (for example 8:00 AM in and 11:00 AM out ) the calculation is correct. How can I dalculatethe Timespan for a period of time statrting in the AM and ending in the PM?
You're just making it hard for yourself by using TextBoxes. Use DateTimePickers like you should and it becomes a doddle:
VB.NET:
Dim time As TimeSpan = Me.endTimePicker.Value.TimeOfDay.Subtract(Me.startTimePicker.Value.TimeOfDay)
 
Back
Top