220 likes | 307 Views
Plan for today: Quickly review the Data data type. Lean about some very useful date functions DatePart DateDiff DateAdd. There is a date/time type in VB. Date A Date variable can hold any date/time from Jan 1, 100 to Dec 31, 9999. Dim dteEamonnsBday As Date dteEamonnsBday = #4/4/1968#.
E N D
Plan for today: • Quickly review the Data data type. • Lean about some very useful date functions • DatePart • DateDiff • DateAdd
There is a date/time type in VB Date A Date variable can hold any date/time from Jan 1, 100 to Dec 31, 9999 Dim dteEamonnsBday As Date dteEamonnsBday = #4/4/1968# This is the American format
The Date variable is really an object. Because dates have multiple parts we may want to access. Consider one of the greatest events in human history, my birth. We may want to find out its… Year, day of week, day of month, day of year, month, was it a leap year?, the minutes, seconds, hours etc So this generically is a date Dim dteEamonnsBday As Date And little modifiers like this… dteEamonnsBday.Year …let us access parts of the date
Dim dteEamonnsBday As Date dteEamonnsBday = #4/4/1968# DateDemo.Text = dteEamonnsBday Dim dteEamonnsBday As Date dteEamonnsBday = #4/4/1968# DateDemo.Text = dteEamonnsBday.ToLongDateString
Dim dteEamonnsBday As Date dteEamonnsBday = #4/4/1968# DateDemo.Text = dteEamonnsBday.Year DateDemo.Text = dteEamonnsBday.DayOfWeek DateDemo.Text = dteEamonnsBday.Month Suppose we want the text “April”, or the day “Thursday”? (why does VB not do this automatically?)
DateDemo.Text = dteEamonnsBday.ToLongDateString Alternative way to get the text for a day of week
Dim dteToday As Date dteToday = Now DateDemo.Text = dteToday “Now” is a function, which takes the no parameters, and returns the current date and time DateDemo.Text = dteToday.ToLongDateString
We can access any part of a date with the following syntax. In each case an integer is returned DateDemo.Text = dteEamonnsBday.Month DateDemo.Text = dteEamonnsBday.Year DateDemo.Text = dteEamonnsBday.WeekOfYear DateDemo.Text = dteEamonnsBday.Quarter DateDemo.Text = dteEamonnsBday.DayOfWeek DateDemo.Text = dteEamonnsBday.Day DateDemo.Text = dteEamonnsBday.DayOfYear DateDemo.Text = dteEamonnsBday.Hour DateDemo.Text = dteEamonnsBday.Minute DateDemo.Text = dteEamonnsBday.Second
Let us consider some useful functions for manipulating dates
You can also try the DatePart function instead of any of the above functions. This function has 2 arguments, the first being a string corresponding to what part of the date you want returned and the other being the date expression. The DatePart function can also return the quarter, the day of the year, and the week of the year etc… DateDemo.Text = "It is " & DatePart("n", Now) & " past the hour." Other acceptable strings to use for the first argument are: • "yyyy" - identical to using Year function • "m" - identical to using Month function • "d" - identical to using Day function • "w" - identical to using Weekday function • “ww” - identical to using WeekOfYear function • "h" - identical to using Hour function • "n" - identical to using Minute function • "s" - identical to using Second function Why do this?
Example of DatePart, timing a section of code. How how does it take VB to count to a billion? Dim dteBegin, dteEnd As Date Dim lngTimer, lngX As Long dteBegin = Now For lngX = 1 To 1000000000 Next dteEnd = Now lngTimer = DatePart("s", dteEnd) - DatePart("s", dteBegin) DateDemo.Text = "It took " & lngTimer.ToString & " seconds"
The DateDiff function can tell you the difference between two dates. Not just the number of days, but any date or time interval. There are three required arguments, the string corresponding to the interval (these are the same strings listed above for use with the DatePart function), and the two dates to compare. Dim dteEamonnsBday As Date Dim lngDays As Long dteEamonnsBday = "4/4/1968" lngDays = DateDiff("d", dteEamonnsBday, Now) DateDemo.Text = "My Age in days is " & Str(lngDays)
Dim dteNextStarWarsMovie As Date Dim lngDays As Long dteNextStarWarsMovie = "5/19/2005" lngDays = DateDiff("d", dteNextStarWarsMovie, Now) DateDemo.Text = Str(lngDays) & " days until the next SW movie." We need to be careful with the order of the paramenters lngDays = DateDiff("d", dteNextStarWarsMovie, Now)
Alternative version of timing program How how does it take VB to count to a billion? Why different? Dim dteBegin, dteEnd As Date Dim lngTimer, lngX As Long dteBegin = Now For lngX = 1 To 1000000000 Next dteEnd = Now ‘lngTimer = DatePart("s", dteEnd) - DatePart("s", dteBegin) lngTimer = (DateDiff("s", dteBegin, dteEnd)) DateDemo.Text = "It took " & lngTimer.ToString & " seconds" Only change is here
When comparing December 31 to January 1 of the following year, DateDiff returns 1 for Year, Month and quarter. In most cases this makes sense for business and legal uses. However, if you were speaking on New years day, you would not say that a baby born the day before was 1 year old. You need to be careful about the semantic, cultural and legal meaning of date calculations.
The DateAdd function can add or subtract date or time intervals to a particular date. The first argument is the string which represents the interval (same strings as DatePart and DateDiff), the second is the number of intervals to add or subtract (positive numbers for future dates, negative numbers for past dates), and the third is the date to perform the calculation on. When could a baby, born today, first exercise their franchise? Dim dteDOB, dteCanVote As Date dteDOB = Now dteCanVote = DateAdd("yyyy", 18, dteDOB) DateDemo.Text = "You can vote on " & dteCanVote.ToLongDateString
The DateAdd function is very intelligent. It knows about leap years and it knows that all months don't have the same number of days. For example if you're trying to find the date one month after Jan. 31, the function will return Feb. 28 on non-leap years and Feb. 29 on leap years.
How do we detect leap years? Dim lngYear As Long Dim sngRem As Single lngYear = DatePart("yyyy", Now) sngRem = (lngYear / 4) - Int(lngYear / 4) If sngRem > 0 Then DateDemo.Text = "This is NOT a leap year" Else DateDemo.Text = "This is leap year" End If
The previous code is actually incorrect!!!! The year is defined as the length of time it takes to pass from one vernal equinox to another. If the calendar gains or loses days, the date for the equinox shifts. Because the physical year isn't exactly 365.25 days in length (as the calendar says it should be), the current calendar supplies 3 too many leap years every 385 years. To make up for that, years divisible by 100 aren't leap years unless they're a multiple of 400. This means that 1700, 1800, and 1900 weren't leap years, but 2000 will be. The code above would be wrong in 2100.
Dim dteEamonnsBday As Date dteEamonnsBday = #4/4/1968# DateDemo.Text = dteEamonnsBday If dteEamonnsBday.IsLeapYear(dteEamonnsBday.Year) Then DateDemo.Text = "The great sage Eamonn was born on a leap year" End If
If I were studying for the last quiz… • Could you write some code, that given an arbitrary year, say… • shtYear = 2005 • Gives the date of new years day of that year? • 2) Gives the date of thanksgiving of that year? • 3) Calculates how many Tuesdays are in that April of that year?