22
Plan for today: Quickly review the Data data type. Lean about some very useful date functions •DatePart •DateDiff •DateAdd

Plan for today: Quickly review the Data data type. Lean about some very useful date functions

  • Upload
    loring

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

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#. - PowerPoint PPT Presentation

Citation preview

Page 1: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

Plan for today:

Quickly review the Data data type.

Lean about some very useful date functions

•DatePart•DateDiff•DateAdd

Page 2: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

Data Type Prefix

Date dte

Dim dteEamonnsBday As Date

dteEamonnsBday = #4/4/1968#

Date

A Date variable can hold any date/time from Jan 1, 100 to Dec 31, 9999

There is a date/time type in VBThere is a date/time type in VB

This is the American format

Page 3: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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

Dim dteEamonnsBday As Date

So this generically is a date

And little modifiers like this…

dteEamonnsBday.Year

…let us access parts of the date

Page 4: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

Dim dteEamonnsBday As Date

dteEamonnsBday = #4/4/1968#

DateDemo.Text = dteEamonnsBday

Dim dteEamonnsBday As Date

dteEamonnsBday = #4/4/1968#

DateDemo.Text = dteEamonnsBday.ToLongDateString

Page 5: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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?)

Page 6: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

One way to get the text for a day of week

Page 7: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

DateDemo.Text = dteEamonnsBday.ToLongDateString

Alternative way to get the text for a day of week

Page 8: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

Dim dteToday As Date

dteToday = Now

DateDemo.Text = dteToday

DateDemo.Text = dteToday.ToLongDateString

“Now” is a function, which takes the no parameters, and returns the current date and time

Page 9: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

DateDemo.Text = dteEamonnsBday.MonthDateDemo.Text = dteEamonnsBday.YearDateDemo.Text = dteEamonnsBday.WeekOfYearDateDemo.Text = dteEamonnsBday.QuarterDateDemo.Text = dteEamonnsBday.DayOfWeekDateDemo.Text = dteEamonnsBday.DayDateDemo.Text = dteEamonnsBday.DayOfYearDateDemo.Text = dteEamonnsBday.HourDateDemo.Text = dteEamonnsBday.MinuteDateDemo.Text = dteEamonnsBday.Second

We can access any part of a date with the following syntax.

In each case an integer is returned

Page 10: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

Let us consider some useful functions for manipulating dates

Page 11: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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…

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

DateDemo.Text = "It is " & DatePart("n", Now) & " past the hour."

Why do this?

Page 12: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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"

Example of DatePart, timing a section of code.How how does it take VB to count to a billion?

Page 13: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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)

Page 14: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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."

lngDays = DateDiff("d", dteNextStarWarsMovie, Now)

We need to be careful with the order of the paramenters

Page 15: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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"

Alternative version of timing programHow how does it take VB to count to a billion?

Only change is here

Why different?

Page 16: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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.

Page 17: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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.

Dim dteDOB, dteCanVote As Date

dteDOB = Now

dteCanVote = DateAdd("yyyy", 18, dteDOB)

DateDemo.Text = "You can vote on " & dteCanVote.ToLongDateString

When could a baby, born today, first exercise their franchise?

Page 18: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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.

Page 19: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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

Page 20: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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.

Page 21: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

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

Page 22: Plan for today: Quickly review the Data data type. Lean about some very useful date functions

If I were studying for the last quiz…

Could you write some code, that given an arbitrary year, say…

shtYear = 2005

1) 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?