Presentation is loading. Please wait.

Presentation is loading. Please wait.

IMS 3253: Dates and Times 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Date and Time Data Turning Strings into.

Similar presentations


Presentation on theme: "IMS 3253: Dates and Times 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Date and Time Data Turning Strings into."— Presentation transcript:

1 IMS 3253: Dates and Times 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Topics Date and Time Data Turning Strings into Dates Formatting Dates and Times Date and Time Properties and Methods Date Arithmetic Date Intervals Date Time Logical Tests Time, Time, Time, See What’s Become of me While I looked around for my possibilities Paul Simon

2 IMS 3253: Dates and Times 2 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date and DateTime Data VB provides two names for the same data type –Date –DateTime A Date datatype contains the numeric count of the number of 100 nanoseconds (ticks) that have elapsed since Midnight, January 1 st, 0001. –Interpreted as a date and time value by adding this value to 1/1/01 –Datetime precision can therefore be measured down to 1/10,000,000 of a second (We usually don’t need this precision) Dim dtSalesDate as Date DimdtSalesDate as DateTime

3 IMS 3253: Dates and Times 3 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date and Time Data (cont.) The default value of a Date datatype is Midnight, 1/1/01 Sources of datetime values –DateTimePicker.Value property (but not.Text) –Now value returns system date and time Dim dtNow as Date = Now –Today value returns system date only –Dim dtToday as Date = Today –Converting from string literal Dim dtDefault As Date MessageBox.Show(dtDefault.ToString, "First Date")

4 IMS 3253: Dates and Times 4 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Setting DateTime Values from Strings There are several ways to convert a string representation of a date into a date value All techniques throw a System.Format exception if the string cannot be converted to a legal date Dim dtDefault As Date dtDefault = Cdate(txtStartDate.Text) dtDefault = DateTime.Parse(txtStartDate.Text) dtDefault = #12/27/2008# Dim dtStartDate as New DateTime(2008, 12, 27, 12, 32, 8)

5 IMS 3253: Dates and Times 5 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Setting DateTime Values from Strings (cont.) See list of legal date and time formats on p. 265 The IsDate(object) function returns True or False depending on whether object can be converted to a legal date Dim dtDefault As Date If IsDate(txtStartDate.Text) Then dtDefault = DateTime.Parse(txtStartDate.Text) Else MessageBox.Show("The start date is not a valid date", _ "You Nincompoop", MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation) End If

6 IMS 3253: Dates and Times 6 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date and Time Data (revisited) A DateTime variable always contains an entire datetime value –If only the date portion is set the time defaults to midnight of that date (12:00:00 AM) –If only the time portion is set the date defaults to the current system date (DateTime.Today or just Today) Dim dtTest As Date dtTest = DateTime.Parse("2:15 PM") MessageBox.Show(dtTest.ToString) Dim dtTest As Date dtTest = DateTime.Parse("11/19/2008") MessageBox.Show(dtTest.ToString)

7 IMS 3253: Dates and Times 7 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Formatting Dates and Times There are some built in datetime formats as well as custom formatting symbols ListBox1.Items.Clear() Dim dtStart As DateTime = Now With ListBox1.Items.Add(dtStart.ToString).Add(dtStart.ToLongDateString).Add(dtStart.ToShortDateString).Add(dtStart.ToLongTimeString).Add(dtStart.ToShortTimeString).Add(FormatDateTime(dtStart, _ DateFormat.GeneralDate)).Add(Format(dtStart, _ "dd MMMM yyyy hh:mm:ss:fffff")).Add(Format(dtStart, "dd MM yy hh:mm")).Add(Format(dtStart, "MMMM dd, yyyy")) End With 11/19/2008 11:12:14 AM Wednesday, November 19, 2008 11/19/2008 11:12:14 AM 11:12 AM 11/19/2008 11:12:14 AM 19 November 2008 11:12:14:67187 19 11 08 11:12 November 19, 2008

8 IMS 3253: Dates and Times 8 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date and Time Properties

9 IMS 3253: Dates and Times 9 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date and Time Properties (cont.) Dim dtTest As Date = Now Dim intMonth as integer intMonth = dtTest.Month

10 IMS 3253: Dates and Times 10 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date and Time Methods These methods are newcomers to VB and solve a huge problem that used to face programmers –Find the start and end dates of a billing period consisting of a calendar month

11 IMS 3253: Dates and Times 11 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date Arithmetic A huge variety of calculations can be performed on dates Most common business problems –Calculate the interval between two dates How many days late is a payment How old is an employee –Find the datetime a specified interval from another date The due date is 14 days form the checkout date The next appointment should be six months from today

12 IMS 3253: Dates and Times 12 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date Arithmetic (cont.) More Common Problems –Your vehicle registration expires on the last day of the month in which your birthday falls next year –List all purchases made in the last calendar month (or week or quarter or year)—regardless of when the program runs Find the first and last dates of a month

13 IMS 3253: Dates and Times 13 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Adding Intervals to a DateTime Dim dtNextAppt As Date dtNextAppt = Now.AddMonths(6)

14 IMS 3253: Dates and Times 14 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Adding Intervals to a DateTime (cont.) VB is very smart when adding days to leap year months and short months

15 IMS 3253: Dates and Times 15 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Measuring Intervals and the TimeSpan TimeSpan is a value that represents ticks, a period of time Dim tmspnTest As TimeSpan tmspnTest = Now - DateTimePicker1.Value MessageBox.Show(tmspnTest.ToString, "Interval") Days Hours Minutes Seconds Ten Millionths of Seconds

16 IMS 3253: Dates and Times 16 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Intervals and the TimeSpan (cont.) TimeSpan has several useful properties –.Days gives the value of the days portion of the timespan (14) –.Hours  the hours portion (0) –.Minutes  the minutes portion (0) –.Seconds  the seconds portion (04) –.Milliseconds  rounded milliseconds (875)

17 IMS 3253: Dates and Times 17 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Intervals and the TimeSpan (cont.) Additional TimeSpan Properties –TotalDays  Whole and fractional days contained in the timespan value (116.625  116 + 15/24) –TotalHours  Whole and fractional hours in the TS value (2799.033  116 x 24 + 15 + 2/60) –TotalMinutes –TotalSeconds –TotalMilliseconds

18 IMS 3253: Dates and Times 18 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Measuring Intervals For short intervals (less than a year) use the TimeSpan data type –Useful for its fractional values –Maximum unit of measure is the Day –Doesn't handle leap years well when measuring long periods TimeSpanValue / 365 will not necessarily give you the correct number of years

19 IMS 3253: Dates and Times 19 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Measuring Intervals (cont.) The DateDiff(interval, datetime1, datetime2) function deals with longer periods better Accounts for leap years Does not return Fractional Values Dim sglInterval As Single sglInterval = DateDiff(DateInterval.Year, _ #11/19/2008 9:54:23 AM#, #1/23/1946 4:19:00 PM#) MessageBox.Show(sglInterval.ToString, "Years")

20 IMS 3253: Dates and Times 20 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date Time Logical Tests DateTime inequalities work as you would expect If DateTime1 < DateTime2 Then will be True if DateTime1 is earlier than DateTime2 Equality tests can be tricky –You must know the precision with which data is stored –If a sale time is set and saved with the Now value (dtSaleTime = Now) this value will only match an equality test if the datetime value being tested matches in all elements of the date and time value

21 IMS 3253: Dates and Times 21 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Date Time Logical Tests (cont.) If you are only testing for date equality you must use some technique to only compare the date portions of the two datetime values –If dtSaleTime.Date = dtpSearchDate.Value.Date Then


Download ppt "IMS 3253: Dates and Times 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Date and Time Data Turning Strings into."

Similar presentations


Ads by Google