Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Formatting dates with VB.NET and ASP.NET

Total Hit ( 3190)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Format Description Example
d Short Date 
D Long date 
f long date & short time 
F long date and long time 
g short date and short time 
G short date and long time 
M or m month and day 
Y or y year and month 
t short time 
T long time 
s displays in ISO 8601 format using local time 
u displays in ISO 8601 format using universal time 
U date and time in unversal time 
R or r displays in RFC 1123 format 

Custom formatting sequences:
There are also specific character sequences that can be used to achieve custom formatting of dates and times.

Format   Description
d day of month (1 or 2 digits as required)
dd day of month (always 2 digits, with a leading 0 if needed)
ddd day of week (3 letter abbreviation)
dddd day of week (full name)
M month number (1 or 2 digits as required)
MM month number (always 2 digits, with a leading 0 if needed)
MMM month name (3 letter abbreviation)
MMMM month name (full name)
y year ( last 1 or 2 digits, no leading 0)
yy year (last 2 digits)
yyyy year (4 digits)
H hour in 24-hour format (1 or 2 digits as required)
HH hour in 24-hour format (always 2 digits, with a leading 0 if needed)
h hour in 12-hour format (1 or 2 digits as required)
hh hour in 12 hour format
m minutes (1 or 2 digits as required)
mm minutes (always 2 digits, with a leading 0 if needed)
s seconds (1 or 2 digits as required)
ss seconds
t first character in the am/pm designator
tt am/pm designator
z time zone offset, hour only (1 or 2 digits as required)
zz time zone offset, hour only (always 2 digits, with a leading 0 if needed)
zzz time zone offset, hour and minute
/ default date separator
: default time separator
\ escape characters



Using the format specifiers:
Create a new aspnet page and add the control to the page. Assign an id to the control and set runat="server". Your page should resemble the following:

<%@ Page Language="vb" %>

 
   Formatting Dates
 
  
    
  


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.


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:


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.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.