vb Count Up From Time (Last Updated: ** Seconds/Minutes Ago) - vb.net

Okay, I hate just posting a question with no code, but I literally have no idea how to get started other than to user a Timer. I have a Visual Basics program which works with an SQL Table. I'd like to have a label which shows the user how 'old' their display of the table is.
Each time they edit or refresh the table a label is refreshed with a current time stamp. Formatted as 03:19;27.
However, I'd like to show how many seconds or even just minutes the table was last updated.
For example: 'Last updated: 22 Seconds Ago' OR 'Last Updated: Less Than A Minute Ago'.
How could I do this?

The TimeSpan object provides all of the properties you need to format the message however you want. The formatting options in it's ToString method may also be very helpful to you. To get a TimeSpan object, you can simply subtract one Date from another, like this:
Dim span As TimeSpan = lastQueryDate - Date.Now
Dim message As String = String.Format("Last Updated: {0} Seconds Ago", span.TotalSeconds)
Or, if you want to use a Stopwatch object, you could get the span of time since the stopwatch was started by accessing its Elapsed property, which is also a TimeSpan object.

Related

Count down clock - getting the difference between two times

stuck on a simple one.
I have an project that will count down to a start time, the start time is generated from a datetime picker,("HH:mm"), when a button is clicked the time .now is subtracted and the timespan is passed to a timer for the countdown.
a rough outline of the code is
Aa4 is the output from the DateTime picker
Dim Span3
Dim TimetoStart as date
TimetoStart = TimeOfDay.Date 'get current time
Span3=StartTime.Subtract(Aa4) 'Subtract current time from output from datetime picker
LaBel4.text=span3 'Output to label (will be timer later for count down)
Error generated is system.invalidcastexception 'conversion from type timespan to type string is not valid.
As I said at the start it will be easy, I am not seeing it and its doing my head in, cheers all Daz

vb.net program to register time on multiple runners

I building a program to track the time of up to 60 runners. My idea is to use a DataGridView with columns for:
ID (DataGridViewTextBoxColumn)
Name (DataGridViewTextBoxColumn)
Start time (DataGridViewButtonColumn)
Stop time (DataGridViewButtonColumn)
Reset time (DataGridViewButtonColumn)
Finish time (DataGridViewTextBoxColumn)
Adding columns 1-5 including functions is no problem, but I cannot wrap my head around how to display a stopwatch in column 6.
My idea was to add a timer and stopwatch function every time a new row I added, but do I need a timer for each row? If so how do I set the ID of that timer, so I can call the Tick event and display the time in column 6?
Also I assume that I need a stopwatch for each row? So how do I set the name of ID of each Stopwatch - can seem to find any property for that?
See below screen shot of what I am trying to accomplish.
Thanks in advance.
Screen shot

Count timer for a Quizz on VBA

I'm currently trying to install timer in VBA - I'm working on a quizz.
There are several questions to answer. What I would I like to know is the time that someone spends answering each question. So the timer should start every time someone clicks on question, then stop when someone clicks on question 2 and start again instantly.
OnClick events. Just use a DateDiff to calculate the time spent. Of course, they'd have to click a button to begin the exam, or open the form that contains the exam, so there's an initial reference point. But it should only take a few milliseconds to write that timestamp to a table and then calculate the difference each time the button is clicked.
You can use the Timer function. It will give you a precision up to 1/1000 of a second:
Dim myTime as variant
myTime = Timer
...
Debug.Print "Process duration", Timer - myTime

Adding two different dates in VB.net, without using 'Now'

I have been looking through a bunch of different posts on here and on other sites for adding two dates together, but for some reason everyone wants to use 'Now'
I would like to know how to add two different dates together when neither of which are now!
I have tried a few things but I am getting casting errors. Also worth noting I am setting it to the value of a dateTime picker on my page.
MaxDate.Value = MinDate.Value + TimeSpan.FromDays(1)
'does not work
MaxDate.Value = Now + TimeSpan.FromDays(1)
'does work!
If it is not obvious, I have two date pickers on my page and when a radio button is clicked I want to set the 'End date' (maxdate.value) to whatever the 'Start date' (mindate.value) is, and add one day to it.
Thanks for the help!
Just do this:
MaxDate.Value = MidDate.Value.AddDays(1)
You don't need create a TimeSpan object for that because the Date type has built-in methods for doing incrementation by day, month, year, minute, etc.
The AddDays method does not alter the original date, it just returns a new Date object with the offset value.
By the way, if you want to subtract a day, there is no MinusDays method, just do x.AddDays(-1).

VB time input, a better way to do it?

Working on a handy program for my boss at work as a summer project in between semesters both to make life easier for her, and to practice and improve my skills. Intent is to allow her to quickly and easily calculate the hours and minutes the person worked. The form itself has a series of text boxes for the Clock in and Clock out time for each day that week. Currently it attempts to convert the txtbox text into a Date variable, then .Subtract()'s the start from the end and stores it in a rolling total variable which is displayed at the bottom of the form. I can't help but think there is a better way of going about doing this, and I'm absolutely certain that having the below block of code 21 times (7 days, 3 shifts) is inefficient.
Dim StartTime As Date
Dim EndTime As Date
Dim Worked As System.TimeSpan
Dim WorkedTotal As System.TimeSpan
If chkFirst.Checked = True Then
StartTime = CDate(txtMonStart.Text)
EndTime = CDate(txtMonEnd.Text)
EndTime = EndTime.AddHours(12)
Worked = EndTime.Subtract(StartTime)
lblMonWork.Text = Worked.ToString()
WorkedTotal += Worked
Currently it works, mostly. The user has to enter the ":" in the time input, and if the total exceeds 24 hours, it displays a day column (40 hour 12 min work week displays as 1.16:12). I'd like to eliminate both of these unplanned features and allow for my input validation to take place when the focus changes to another box. A changing bgcolor would indicate an invalid input.
Any bright ideas?
Instead of using TextBox for the time input, use DateTimePicker, just change the Format property to Time.
Handle the Validating event of the DateTimePicker and if it's invalid just set e.Cancel = False, that way they can't save changes unless it's valid. Or if you want them to be able to leave the DateTimePicker and just change the colour, just handle the ValueChanged event instead.
Regarding your code sample, I haven't really looked at the logic of it, but instead of having the same code 21 times, just move the code into a separate function and then call that function with the start and end times as parameters and it can return the workedtime as it's return value.
I'm not sure what your UI looks like but if you're repeating the start time and end time input control multiple times as well it might be worth looking at creating your own usercontrol that can contain one each of the start and end time controls, and then you could have the validation be inside that user control so you don't have to have lots of event handlers etc.