Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: ASP.NET .NET Formatting Numbers, Dates, and Times in ASP.NET Tutorial

One of the nice things about VBScript was its built-in formatting functions, such as FormatCurrency, FormatNumber, FormatDateTime, etc. Read on to learn how to use these functions.

Tutorial Details:

ASP FAQS : ASP.NET

Question : How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.

Answer : One of the nice things about VBScript was its built-in formatting functions, such as FormatCurrency , FormatNumber , FormatDateTime , etc. (For more information on these functions see this FAQ or, better yet, check out the Strings FAQ Category .)

When creating ASP.NET Web pages with VB.NET, you can still use these functions, although you will need to import the Microsoft.VisualBasic namespace. But what if you are using C#, or want to start using the new formatting methods available in .NET? To allow for formatting, the .NET Framework contains a general String.Format static method along with .ToString() methods for each object.
So, first off, you can easily convert a non-string data type to a string by using it's .ToString method. That is, if you wanted to turn a DateTime variable into a string you could do:

'Create a var. named rightNow and set it to the current date/time
Dim rightNow as DateTime = DateTime.Now
Dim s as String 'create a string
s = rightNow.ToString()
This simple code snippet creates a DateTime variable ( rightNow ) that is assigned the current system date/time. A String variable ( s ) is then created and assigned the string representation of the DateTime variable.
The .ToString() contains an overloaded variant that accepts a single String parameter. This parameter can be used to specify how to format the DateTime variable. For example, if we wanted to display the date as a three-lettered month namethen the day, and then a comma followed by the year in four digits (like Jan 30, 2002), we could use the .ToString( formatString ) method like so:
'Create a var. named rightNow and set it to the current date/time
Dim rightNow as DateTime = DateTime.Now
Dim s as String 'create a string
s = rightNow.ToString("MMM dd, yyyy")
Pretty neat, eh? Of course the question still remains: "How in the world did I know to use MMM to display the three-letter abbreviation of a month? The answer, of course, is to hit the documentation. One thing the .NET Framework has an abundance of is documentation. For a list of special formatting characters for date/time purposes, see DateTimeFormatInfo Class .


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
ASP.NET .NET Formatting Numbers, Dates, and Times in ASP.NET Tutorial

View Tutorial:
ASP.NET .NET Formatting Numbers, Dates, and Times in ASP.NET Tutorial

Related Tutorials:

JavaWorld - Net News Central
JavaWorld - Net News Central
 
XSL gives your XML some style - JavaWorld June 2000
XSL gives your XML some style - JavaWorld June 2000
 
Filter code with Servlet 2.3 model - JavaWorld June 2001
Filter code with Servlet 2.3 model - JavaWorld June 2001
 
Matchmaking with regular expressions - JavaWorld July 2001
Matchmaking with regular expressions - JavaWorld July 2001
 
Working in Java time
Working in Java time
 
Container-managed relations for the 21st century
Container-managed relations for the 21st century
 
Rumble in the jungle: J2EE versus .Net, Part 1
Rumble in the jungle: J2EE versus .Net, Part 1
 
Eclipse casts shadows
Eclipse casts shadows
 
Rumble in the jungle: J2EE versus .Net, Part 2
Rumble in the jungle: J2EE versus .Net, Part 2
 
Profiling CPU usage from within a Java application
Profiling CPU usage from within a Java application
 
Simple classes for JDBC
Simple classes for JDBC
 
Light-Weight Visual Components Library for different platform: SWT, J2SE, J2ME, .NET
Light-Weight Visual Components Library for different platform: SWT, J2SE, J2ME, .NET
 
JTimepiece
JTimepiece is the advanced library for working with dates and times in Java. Many easy-to-use methods in this API make it easy for any developer, from beginner to expert, to use JTimepiece.
 
Jeff Schmitt's JDBC Page
This tutorial assumes you are using the MySQL database and the GWE JDBC drivers. The host computer is triton.towson.edu.
 
FOP is the world's first print formatter driven by XSL formatting objects.
It is a Java application that reads a formatting object tree and then turns it into a PDF document. The formatting object tree, can be in the form of an XML document (output by an XSLT engine like XT or Xalan) or can be passed in memory as a DOM Document
 
JavaServer Pages Technology - Documentation
Sun's tutorial for Java Server Pages that provide a good introduction to design web pages with JSP.
 
Tag Libraries Tutorial
This tutorial describes how to use and develop JavaServer Pages tag libraries. The tutorial assumes that you know how to develop servlets and JSP pages and are familiar with packaging servlets and JSP pages into Web application archives.
 
Domain Registration Guide
Domain Registration Guide Domain Registration Guide What is Domain? Internet is the large network of computers. On the Internet there should be some way to identify the computers. Domain name is the solution for this. Domain Name is the unique
 
Open Source Web Frameworks in Java
Open Source Web Frameworks in Java Open Source Web Frameworks in Java Struts Struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP. Struts is maintained as a part of Apache Jakarta project and is open
 
What is WAP? Detailed discussion of WAP API with examples.
What is WAP? Detailed discussion of WAP API with examples. Learn WAP in 60 minutes W ireless Application Protocol or WAP for short, allows the developers to develop next generation web application for cellular devices. Through WAP enabled mobile
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.