If you want to display the time in a label for example, you might use something like this:
lblCurrentTime.Text = Now
This will retrieve the current time and date from the computer and display it in the label lblCurrentTime. If you only want to display the time though, you would need to do it like this:
lblCurrentTime.Text = Now.ToString("HH:mm:ss")
This will display the time in Hours, Minutes and Seconds. It will only show the time when the program starts though, it won't keep updating the time to keep it current. If you want it to act like a clock, you need to add a Timer function and put this code in to the function as well.
I have been posting a little bit on this function in recent times, relating to my sleep calculator. What I'm going to do is use this function to create a real, bonafide sleep calculator, but that will have to wait until the workload of assignments has thinned out a little bit.
Anyway, what other methods can be added to the Now function? So far ToString has been added. According to the list in Intellisense, the following can be added as methods to the Now function:
- Add (AddDays, AddHours, AddMilliseconds, AddMinutes, AddMonths, AddSeconds, AddTicks, AddYears)
- To (ToString, ToLongTimeString, ToOADate, ToShortDateString, ToShortTimeString, ToUniversalTime, ToBinary, ToFileTime, ToFileTimeUtc, ToLocalTime)
- TryParse, TryParseExact
- Compare, CompareTo
- DaysInMonth
- Equals
- From (FromBinary, FromFileTime, FromFileTimeUtc, FromOADate)
- Get (GetDateTimeFormat, GetHashCode, GetType, GetTypeCode)
- IsDaylightSavingTime, IsLeapYear
- Parse, ParseExact
- ReferenceEquals
- SpecifyKind
- Subtract
Good old Intellisense! I guess I must have a modicum of Intellisense myself as I looked there first :o).
ReplyDeleteWell done Deb.
ReplyDeleteMind you, if there is a hard way to do something, I will find and use it. It's uncanny really. :-)