Professional Documents
Culture Documents
Formatting Dates Times and Numbers in ASP - Net - 4GuysFromRolla
Formatting Dates Times and Numbers in ASP - Net - 4GuysFromRolla
Formatting Dates Times and Numbers in ASP - Net - 4GuysFromRolla
Recent Articles
Published: Wednesday, November 10, 2010
All Articles
Web Hosts
methods I've created and use in my own projects. There's also a demo application you can download that lets you specify a
culture and then shows you the output for the standard format strings for the selected culture. Read on to learn more!
XML
Information: - continued -
Advertise
Feedback
Author an Article Formatting Basics
There are a couple of different ways to format a DateTime or numeric variable. The first way involves using the ToString method,
which has a number of overrides. For DateTime variables:
• DateTime.ToString() - converts the value of the DateTime to a string representation using the current culture and the "G"
format string. (See the table below to see the output of the "G" format string.)
• DateTime.ToString(string) - converts the value of the DateTime to a string representation using the current culture and
the specified format string.
• DateTime.ToString(IFormatProvider) - converts the value of the DateTime to a string representation using the specified
culture settings and the "G" format string.
• DateTime.ToString(string, IFormatProvider) - converts the value of the DateTime to a string representation using the
specified culture settings and the specified format string.
Numeric variables - integers, doubles, decimals, and so on - have the same ToString method and ToString overloads.
Formatting can also be performed using the String.Format method. The String.Format method accepts as input a format string
that contains a number of placeholders, followed by the values to plug into those placeholders. Placeholders take the form {0},
{1}, and so on, and may optional include a format string like so: {0:formatString}. The following code snippet shows using the
String.Format method with three placeholders. The values plugged into these placeholders are a string and two decimal values
that are both formatted as currencies using the "C" format string within the placeholder syntax.
// OUTPUT FOR EN-US CULTURE: Tito, your order subtotal is $245.13. Tax is $18.94.
string output = string.Format("{0}, your order subtotal is {1:c}. Tax is {2:c}.", customerName,
subTotal, taxTotal);
In the example above, the String.Format method uses the current culture. You can optionally supply the culture settings as the
first parameter to the String.Format method.
Full date/time pattern - short time (f) DateTime.Now.ToString("f") Monday, November 08, 2010 3:39 PM
Full date/time pattern - long time (F) DateTime.Now.ToString("F") Monday, November 08, 2010 3:39:46 PM
RFC 1123 pattern (R) DateTime.Now.ToString("R") Mon, 08 Nov 2010 15:39:46 GMT
Universal full date/time pattern (U) DateTime.Now.ToString("U") Monday, November 08, 2010 11:39:46 PM
Keep in mind that the output of the standard format strings depend on the culture settings. For example, formatting November
8th, 2010 using the short date pattern displays 11/8/2010 when using the en-US culture; however, the French (France) culture
(fr-FR) displays 08/11/2010; German (Germany) shows 08.11.2010; and the Japanese culture outputs 2010/11/08. The
download available at the end of this article provides a web page with a drop-down of all available cultures. You can select a
culture and see the output for all of the standard date and time format strings.
If you do not explicitly specify a culture when formatting the .NET Framework uses the current culture settings. For an ASP.NET
application, these are the culture settings defined on the web server, not the culture settings of the user visiting your site. To
learn how to override the web server's culture settings, either for the entire application or on a page-by-page basis, refer to How
to: Set the Culture and UI Culture for ASP.NET Web Page Globalization.
Day of the week name plus month name and day of month DateTime.Now.ToString("dddd, MMMM d") Monday, November 8
As with the standard format strings, the individual pieces of the custom format strings are culture-specific. The day of the week
names and month names, for instance, differ based on the culture.
These custom formatting methods of mine are implemented as extension methods. Extension methods allow for a developer to
seemingly add methods to an existing type (such as the DateTime type). Consequently, my methods - ToRelativeDateString
and ToUniversalFormat - look as if they're methods defined on the DateTime structure rather than methods I created. This is the
beauty of extension methods. In the demo available for download these extension methods can be found in the App_Code folder.
Some of the code snippets below reference DateTime.Now, which returns the current date and time. The output column shows the
results when this code was executed on my computer at November 8th, 2010 at 3:39 PM.
Relative time for date five days DateTime.Now.AddDays(-5).ToRelativeDateString() five days ago
ago
DateTime.Now.AddHours(-3.14159).ToRelativeDateString()
Relative time for several hours ago three hours ago
Relative time for more than five DateTime.Now.AddMinutes(-6.25).ToRelativeDateString() six minutes ago
minutes ago
Relative time for less than five DateTime.Now.AddMinutes(-2.5).ToRelativeDateString() a few minutes ago
minutes ago
Relative time for within past DateTime.Now.AddSeconds(-15).ToRelativeDateString() less than a minute ago
minute
Universal format for date in other var birthDate = new DateTime(2008, 9, 1 9, 45, 0); Monday, Sep 1, 2008 at
year birthDate.ToUniversalFormat() 9:45 AM
As with the date and time format strings, the numeric formatting is culture-sensitive. Different cultures use different grouping
separators (commas vs. spaces vs. periods), different currency symbols, and different decimal place characters (periods vs.
commas). Be sure to download the demo to view how formatted numbers look differently with alternative culture settings.
Note the selection separator format string, which allows you to specify different formats for when a number is positive, negative,
and zero. The format string #,000.00;(#,000.00);Zero says, "Use the format string #,000.00 when the number is positive,
(#,000.00) when it's negative, and Zero when it's zero." The last three rows in the grid below show how the appropriate format
string is applied based on the value of the number being formatted.
Conclusion
Formatting is a common task in an ASP.NET application and, not surprisingly, the .NET Framework offers a number of techniques
for formatting both DateTime and numeric variables. Formatting a date, time, or numeric value involves two parameters - the
culture settings and a format string. If omitted, the current culture is used and a default format string is assumed. The format
string can be either a standard format string or one built from the various custom format strings. Additionally, you can create your
own formatting methods for scenarios that cannot be handled by the existing formatting strings.
Happy Programming!
• By Scott Mitchell
Attachments:
• Download the Demo Code Used in this Article
Further Reading
• Standard Date and Time Format Strings
• Custom Date and Time Format Strings
• Standard Numeric Format Strings
• Custom Numeric Format Strings
• Extending Base Type Functionality with Extension Methods