Calendars, DateTime, Variables, and performing calculations on dates WSU – MIS171 Mauricio...
-
Upload
hilda-underwood -
Category
Documents
-
view
238 -
download
0
Transcript of Calendars, DateTime, Variables, and performing calculations on dates WSU – MIS171 Mauricio...
Calendars, DateTime, Variables, and performing calculations on dates
WSU – MIS171Mauricio Featherman, Ph.D
Working with Dates
• We will look at the DateTime class which includes a type(variable), properties, & methods,
• We’ll use methods of the DateTime variable (add, subtract)
• We’ll use a Timespan variable which holds results of datetime calculations (one date minus another – accountants love this!)
• We’ll implement some windows calendar controls that make incorporating dates easier
My computer knows what day it is?
• You can pull the current date, time or both into a label easily by using the built-in Now function
• Label1.text = Now.tostring gives you the date and time (10/12/2006 2:51pm)
• Label1.text = Now.toShortDateString gives you just the date (10/12/2006)
• Label1.text = Now.toShortTimeString gives you just the date (2:51 pm)
Useful Windows Controls for Working With Dates
• DateTime picker
• MonthCalendar Controls
• Date Time Picker (e.g., dtpEventDate) provides a slim listbox style control that expands to allow selection from a calendar
When you select a date in the control it captures the date in the control’s .value property
• The selected day from the dtp (the dtp.value) are usually assigned to a DateTime variable (dim dtm as dateTime = dtp.value), displayed in a label (label1.text = dtp.value.tostring) or stored in a column of a row in a datatable dr(“EventDate”) = dtp.value
Date Time Picker
Retrieving the Selected Date from a dtp
• Private dtmStart As DateTime
Private Sub DateTimePicker1_ValueChanged()
dtmStart =DateTimePicker1.Value
‘Then you can show it in a label or do other things with it lblStartDate.Text = dtmStart.ToString
End Sub
• MonthCalendar’s allow you to choose one date or a range of dates.
• Use the maxSelectionCount property to the # of days you want a user to be able to select
• Use the DateChanged() event to capture the start and end date
• If you select only 1 day the start and end date are the same
MonthCalendar
• You can either do something with the dates (store them as daes and/or do timespan calculationsPrivate)
Private Sub calMonth_DateChanged()
lblStartDate.Text = MonthCalendar1.SelectionStart.ToString
lblEndDate.Text = MonthCalendar1.SelectionEnd.ToString
End sub
Retrieving the Selected DateFrom a MonthDate Calendar
Later we’ll use timespan to subtract the start date from the end date to count up how many days are in the range
TimeSpan Class
• TimeSpan is an Class used to hold the result of a DateTime calculation (Ex: Dim ts as TimeSpan)
• TimeSpan has properties:
intDays = ts.TotalDays ‘used to display the # of days intHours = ts.TotalHours ‘used to display the # of hours
ts = MonthCalendar1.SelectionEnd.Subtract(MonthCalendar1.SelectionStart)
ts = Now.Subtract(dtpEvent.Value)
lbldays.Text = ts.TotalDays.ToString & " days"
In these examples the number of days selected in the monthcalendar control are displayed or the number of days since a date selected in a dtp
TimeSpan?
Dim ts as timespan ts = Now.Subtract(CDate(drv("Date_Loaned")))
Select Case ts.Days Case 7 To 13 Case >21End Select
Timespan is a special type of variable that can hold the results of a calculation where you add or subtract one date from another. Above ts stores how many days ago its been since the item was loaned out. The timespan variable created (above ts) can display the # of days
Ok – let’s kick it up a notch
• Lets say you have a table in adatabase that have a column thatis a date/time datatype. This means you can perform a timespan calculation on the value for each row of a datatable
• Perform a timespan for every row in a datatable like this:
Dim drv as datarowviewFor each drv in dvLoanedOut
Dim ts as timespants = Now.Subtract(drv(“Date_Loaned”))Messagebox.show(“The item was loaned out “ & ts.days & “ days ago.”)
Next
OK – now lets put it all together – first here’s how you pull values from columns on a datatable to build the to and from fields• For Each drv In dv
• 'shoot an email to everyone•
Dim str as string• Dim NewMail As New MailMessage•
NewMail.To = drv("Email").trim NewMail.From = “[email protected]"
• NewMail.Subject = "Please return the " & drv("type") & " you borrowed“
str = "Dear " & drv("Loaned_to") & vbCrLf & vbCrLf & "Please return the " & drv("Title") & " " & drv("Type") & " that you borrowed on " & drv("Date_Loaned") & "." & vbCrLf & vbCrLf & "Thanks, Butch“
• NewMail.Body = str• SmtpMail.Send(NewMail)
Next
Since you’re looping each row of the Dataview, you can access the value in any column for each row, pulling out its value and pushing it into a propertyof the email or into a string
Example code to send different emails depending on the # of days the item has been loaned out (timespan)
• For Each drv In dv• Dim NewMail As New MailMessage
… ts = Now.Subtract(CDate(drv("Date_Loaned")))
• Select Case ts.Days• Case 7 To 13
str= build a string to send a different message Case 14 To 20 str = "Dear " & drv("Loaned_to") & vbCrLf & vbCrLf & "You have had my " & drv("Title") & " " & drv("Type") & " for " & ts.Days & " days. This is way too long, please return it today!" & vbCrLf & vbCrLf & "Thanks, Butch" Case >21 str= build a string to send a different message
End Select• NewMail.Body = str
SmtpMail.Send(NewMail)
Depending on the # days calculated and Stored in timespan, you can fire off differentformated emails (by using a select case within a for each loop).
Example output
Summary
• Putting a few building blocks together (loops, timespan, SMTP, dataViews) you can write a powerful application that can for example with 1 push of a button send reminder emails to thousands of people, if there contact info resides in a database table
• For example you send a reminder to pay a bill, register for a class, return borrowed money, attend a study session.
• Each email reminder that is sent can be personalized with the recipients name, address, account and info. So this is e-mail-merge, sending customized form e-letters to many people. Master this and pitch these skills to a marketer and don’t be surprised if they hire you for a con$ulting project!