Dates and Time - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Dates and Time

Description:

The simplest way of obtaining the current system date is to use the ... If Date1 = Date2 Then. MessageBox.Show('The dates are the same.') End If. 6. Now ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 11
Provided by: matt131
Category:
Tags: date1 | dates | time

less

Transcript and Presenter's Notes

Title: Dates and Time


1
Dates and Time
  • We shall look at
  • Getting the current system date.
  • Getting the current system time.
  • Comparing dates.
  • Comparing times.

2
Getting the current system date.
  • The simplest way of obtaining the current system
    date is to use the DateString function.
  • The DateString function returns the date of the
    computer as set in the system's control panel.
  • So for example
  • txtTheDate.Text DateString

3
Getting the current system time.
  • The simplest way of obtaining the current system
    time is to use the TimeString function.
  • The TimeString function returns the time of the
    computer as set in the system's control panel.
  • So for example
  • txtTheTime.Text TimeString
  • Would display the current time in a text box
    called txtTheTime.

4
The Date Data Type.
  • The Date data type is what we use when we want to
    store either date or time values (there is no
    explicit Time Data Type in VB!)
  • To declare a variable as Date you would do the
    following...
  • Dim MyDateVar as Date

5
Comparing dates.
  • Date comparisons may be performed much like any
    other data type, the following code is an
    example...
  • Dim Date1 As Date
  • Dim Date2 As Date
  • Date1 1/2/66
  • Date2 3/2/55
  • If Date1 Date2 Then
  • MessageBox.Show("The dates are the
    same.")
  • End If

6
Now
  • One function provided by VB is the function Now.
  • The problem with DateString (and TimeString), as
    the name suggest, is that they return a data type
    of String.
  • This creates a problem when performing date
    comparisons as comparing a String with a Date
    could potentially result in a crash.
  • Now returns a value of Date data type but with
    one small problem!
  • The value of Now incorporates both the Date and
    the Time!
  • So the following is a valid example of data
    returned by Now...
  • 31/1/2005 111923

7
Problem
  • The problem with this is that it makes a
    comparison with a simple date value a little more
    tricky as we cant simply, for example, compare a
    date, e.g.
  • 31/1/2005
  • with the value returned by Now.
  • 31/1/2005 111923

8
The Date Property
  • For example the following code...
  • Dim MyDate as Date
  • MyDate Now
  • MessageBox.Show(MyDate.Date)
  • Would display...
  • 31/1/2005
  • Even though the actual value of MyDate is...
  • 31/1/2005 111923

9
Comparing Times.
  • Comparing times has some of the same problems as
    comparing dates but with a few extra
    complications.
  • Simple time comparisons are not a problem as
    follows...
  • Dim Time1 As Date
  • Dim Time2 As Date
  • Time1 1115
  • Time2 1214
  • If Time1 Time2 Then
  • MessageBox.Show("The times are the
    same.")
  • End If

10
Comparing Times.
  • To compare a time value with Now, we need to
    compare the hours first, then compare the
    minutes, like so...
  • Dim MyTime as Date
  • MyTime 1137
  • If MyTime.Hour Now.Hour Then
  • If MyTime.Minute Now.Minute Then
  • Messagebox.Show("Times are the same")
  • End If
  • End If
Write a Comment
User Comments (0)
About PowerShow.com