Formatting dates with VB.NET and ASP.NET
This tutorial covers formatting dates. To follow this
tutorial, there are some basic requirements:
- A text editor or a visual web design program with HTML
editing capabilities installed on your computer since this
exercise will require hand coding. Examples include Notepad,
Homesite, or Visual Studio.Net
- Access to a server that has the Dot Net Framework
installed. This is necessary to see the results of the
formatting. If you are planning on using your local machine
as a test server, you must use Windows 2000 (any version) or
Windows XP Pro as your OS. The Dot Net Framework can be
downloaded from http://msdn.microsoft.com/netframework/prodinfo/getdotnet.asp
- A basic familiarity with VBNET and ASPNET is
recommended. To get up to speed on ASPNET, there are some
excellent articles at
www.4guysfromrolla.com
Standard Format Specifiers for Dates
and Times:
The table below shows the standard date and time formatters.
Custom formatting
sequences:
There are also specific character
sequences that can be used to achieve custom formatting of
dates and times.
Using the format specifiers:
Create a new aspnet page and add the <asp:label>
control to the page. Assign an id to the control and set
runat="server". Your page should resemble the
following:
<%@ Page Language="vb" %>
<html>
<head>
<title>Formatting Dates</title>
</head>
<body>
<asp:label
runat="server" id="TheDateTime" />
</body>
</html>
The next step is to write the subroutine
that will display and format the date and time. Add the
following code to the aspnet page just after the closing
title tag.
<script runat="server">
Sub Page_Load(s As Object, e As EventArgs)
TheDateTime.Text
= DateTime.Now.ToString("d")
End Sub
</script>
Save the page and preview. The date should resemble the
format 9/8/2002. Compare your result to the date on this
page
This subroutine sets the Text property of the label control
equal to the current date and time. Then by specifying
"d" as the formatter, only the date portion
is returned. Change the "d" to one of the other
formatters to see how it affects the date and time that
are displayed. For example, change the "d" to
"g". You should now see something that resembles
9/10/2002 5:19 PM
Using the custom formatters:
Now its time to try out the custom formatting sequences.
Lets say you want to display the date in the format 9-10-02.
Using the custom formatters, it is very easy to to do
so. Modify the subroutine you wrote earlier as follows:
<script runat="server">
Sub Page_Load(s As Object, e As EventArgs)
TheDateTime.Text
= DateTime.Now.ToString("MM\-dd\-yy")
End Sub
</script>
Note that today's date is being formatted using the sequence
"MM\-dd\-yy". Save the page and preview. The
date should resemble the format 09-10-02.
In the format sequence, MM provides the month, dd gives
the day of the month, and yy gives the last 2 digits of
the year. The escape character \ must be used if the separator
is to be the dash or any other nonstandard separator. Remember
that the : and / are the standard separators for time
and date respectively.