It s a Date (and Time)


It's a Date (and Time)

You'll often want to work with dates in your pages. You'll want people to enter dates into your forms, and you'll want to display dates back to users later. Dates and times are an essential part of many business transactions, and as more business is done over the web, dates in ASP.NET can only become more important.

The key to dates is the Date type. It can store a precise moment in time (right down to minutes and seconds) – and it gives you nearly 8,000 years before you need to worry about another millennium bug.

Entering a Date and Time

You declare a Date in exactly the same way as any other variable – you just use As Date. It's also pretty easy to define a date value in your code. Remember how we use the quotation marks, "", to define strings so that Visual Basic .NET doesn't think our strings are variable or method names? Similar problems occur if we just put a date into our code – Visual Basic .NET won't know what to do with it. Or at least, it will know exactly what to do with it – but it will be wrong! Try creating a new page called DateTest.aspx, and adding a Label and a Button on separate lines. Double click on the Button and add the following:

Sub Button1_Click(sender As Object, e As EventArgs)  Label1.Text = 3/6/2003 End Sub

Then press F5 to run the page. Click the button, and you should see the label display the value we just entered:

0.000249625561657514

but this isn't really what we wanted. We put in a date, but we've got 3 divided by 6, divided by 2003 – Visual Basic .NET has interpreted our date as a piece of arithmetic, because all it sees are numbers and division operators. The way to tell Visual Basic .NET that the whole thing is a date is to enclose it in # characters, like this:

  Label1.Text = #3/6/2003# 

Visual Basic .NET will interpret this date as March 6, 2003. This will seem perfectly sensible to you if you're in America, but if you aren't then you might have expected June 3 instead. Well, Visual Basic .NET is American and when you define a date in # marks, it should always be in m/d/yyyy format. As a programmer, you just need to get used to it.

As well as entering a date this way, it's pretty easy to convert a string to a date, and this feature means you can express your date in lots of different ways. Here's an example:

  Label1.Text = CDate("3/6/2003") 

CDate()is a good way to convert text that your users enter into dates, but it's quite unreliable; if the user enters 3/6/2003, it's not easy to tell whether they mean March or June. How the computer decides will depend on its regional settings (you can find your regional settings in Start | Settings | Control Panel | Regional Options). If you write your ASP.NET pages on your own computer, and then copy them to a server with different regional settings then the decisions will change. My favorite way around this is to always use a shortened word for the month – such as Jun:

  Label1.Text = CDate("3 Jun 2003") 

Another way to get dates from a user is to use a Calendar control. We'll have a look at this useful control in a minute, but first let's see how to display dates to the user.

Displaying Dates

The way Visual Basic .NET stores dates in memory is very different from the way a human writes them down. When you want to show a date to a human, you have lots of choice about how to display it. You can display the date in words, or as a shortened version with numbers. You can also choose to display the part of the date that signifies the time of day, rather than the day, month, and year.

Try It Out—Different Date Formats

  1. Start a new page called Dates.aspx. Drag on four Label controls and a Button, with carriage returns between each one.

  2. Double-click the button and add the following code:

    Sub Button1_Click(sender As Object, e As EventArgs)  Dim myDate As Date  myDate = Now()  Label1.Text = myDate.ToLongTimeString()  Label2.Text = myDate.ToLongDateString()  Label3.Text = myDate.ToShortTimeString()  Label4.Text = myDate.ToShortDateString() End Sub
  3. Press F5 to run the page. My screen looks like this, but yours may well be different:

    click to expand

How It Works

First of all we create a new Date variable. Then we set it to the current date and time using the Now() function (which simply returns a Date variable for the current time and date).

The next four lines display the date in different ways – and the exact detail of how they display will depend on the 'regional' settings of the server that hosts the ASP.NET page. We can't be completely sure of exactly how our dates will display. You could type in exactly the same code as I did, but get quite a different date format.

The Calendar Control

One reliable method of getting dates from the user is the Calendar control. This is a web control that displays a calendar, and lets the user pick a particular day. (It actually lets them choose whole months or weeks too, but let's take things one step at a time.)

Try It Out—Using the Calendar Control

  1. Create a new page called Cal.aspx, and drag a Calendar, a Button, and a Label onto it, each on a separate line.

  2. Double-click the button and add the following code:

    Sub Button1_Click(sender As Object, e As EventArgs)  Label1.Text = Calendar1.SelectedDate.ToLongDateString() End Sub
  3. Run the page and have a play selecting dates and clicking the button:

How It Works

When you click the button, we're getting the currently selected date, and displaying it in long date format using Label1.

By default, the Calendar loads up to display today's date. If you want to make it jump to a different date, you need to set its TodaysDate property. If you wanted to show people a calendar for July 2005, then you would add the following code:

Sub Page_Load(sender As Object, e As EventArgs)  Calendar1.TodaysDate = #7/15/2005# End Sub

Have a go at adding this to the Page.Load event handler, or set the property on the control when you're in Design view.

Of course a calendar has far more properties and methods than the ones we've seen here, but we've covered some of the useful ones. We can play with the other properties using the Properties window in Web Matrix – we get a lot of control over the appearance and behavior of our calendar controls.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net