Click on these links to go the the section you need------>>>>>
Some examples of how to find the system date:
CODE NEEDED FORMAT OUTPUT
Date.Now 6/12/2022 12.35.23
Date.Today 6/12/2022
Now 6/12/2022 12.35.23
Now.Date 6/12/2022
Now.Minute 35 (i.e. 35 minutes)
Now.Day 6 (i.e. the 6th day of the month)
Now.Second 23 (i.e. 23 seconds)
How do we find out today's date? This is known as the system date and we can pick it up from our computer.
You can display today's date by using a timer which runs every second. Here's how:
Choose a timer object from your toolbox and drag it onto your form
2. Change the properties in your timer
Set Enabled = True
Set Interval to 1000 (this means the clock changes every 1000 milliseconds = 1 second)
3. Double click the timer and add the following code:
lblClock.Text = Date.Now
Save and run your program. If the date and time are not showing in full then resize your label
What if we want to check that a customer is old enough to sign up?
(e.g. customer aged 16 cannot sign up for driving lessons)
There is a function called DATEDIFF. We give it the two dates we want to work out the difference between, plus the interval type, which in this case is the number of years between the two dates.
NOTE: we can work out any date or time interval between two dates - see the table to the left------->
So what does the code look like?
Well, if we want to work out if someone is old enough to do something, we can first find the difference in years between their DOB and today's date. (Remember to put the earlier date first).
The code will look like this:
Once we have our number of years calculated, we can then check to see if that person is over the required age or not.
Other useful DateDiff examples:
Use DateDiff with the “d” or “y” interval type parameter to find the number of days between two dates:
DateDiff ("d", #10/7/1999#, #10/10/1999#)
Returns 3.
Use DateDiff with the “yyyy” interval type parameter to find the number of years difference between two dates. This use of DateDiff is the same as finding the difference between the year of endDateTime and the year of startDateTime.
DateDiff ("yyyy", #10/7/1999#, #2/10/2005#)
Returns 6.
NOTE: You can enter actual dates or variables which contain the dates. When entering an actual date, you must place a hash at the begininng and end of the dates.However, the dates must be in American format i.e. "MM/dd/yyyy"
We might want to calculate a date one year in advance of today. Basically, we use today's date (Now.Date) and add on 365 days.
Obviously, this code can be tweaked to add on different timeframes, plus it can also be used to take away days aswell (e.g. what was the date this time last week?)
We can also use the DateAdd function to add or subtract a number of days from a date:
The DateTimePicker in your toolbox will give you a calendar, so that you can click on a date, rather than having to type the date in the correct format.
Choose a DateTimePicker from your toolbox and drag it onto your form.
When you run your program, click on the dropdown box, and it will look like this---->
The chosen date (09/06/2022 in the above example) is stored in the .text property of the DateTimePicker
e.g. lblDateChosen.text = DateTimePicker.text 'takes the chosen date and displays it in a label
Note however, that the default format for DateTimePicker.text is 9th June 2022 (for the chosen date above). If we want to reformat the datetimepicker date, we need the following code:
Sometimes it is useful to put dates chosen in a datetimepicker into a date variable, so they can be compared with dates found in a textfile. We would need to use the code above, plus create a date variable (see StartDate):
There are lots of other custom formats for the datetimepicker.
If you format the datetimepicker to show time format, you could use the following code to calculate the difference between two times, in hours, minutes or seconds:
If you want to find today's name, the code below will do that for you.
Now.DayOfWeek will give you the day number (Note, Sunday = 1, Monday - 2, etc.)
WeekDayName converts the DayOfWeek number to the name of today.