|
Formatting a Date String (ASP.Net) |
|
|
Written by: Evan Cummings
Formatting a Date StringA few months ago I was eagerly setting up a news posting system, and one of the standard features is, of course, a date stamp. For my particular solution I was working with a MySQL 5.0 database, which happens to provide a handy function called CURDATE(), which returns the current date of the server in the mm/dd/yyyy format. Perfect, thats exactly what I want. Working within a DataList control, this is a simple in-line databinding scenario:
<asp:Label ID="lblDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "story_Date") %>'></asp:Label>
But the result isn't quite what you would expect - this statement produces 1/01/08 12:00:00 AM. Where did the timestamp come from? Its easy to check that the dates are in fact being stored in the correct format within MySQL.
The problem lies in the way that ASP.NET interprets dates. The default format includes the long date format plus time stamp. This means, that no matter what format is stored within the database, ASP.NET will append a timestamp. There is no timestamp data available, however, so we get the first time possible - 12:00:00 AM.
This problem is easily solved with the magic of string formatting. The .NET framework provides an immense amount of string formatting options, and we can harness this flexibility to produce the desired data format we expected in the first place. Amongst the myriad of formatting choices, we can simply use a single formatting character to solve this problem. (It is worth noting that the formatting options available extend the amount of control you have over formatting to great lengths, but for simplicity's sake we will examine a basic solution.)
<asp:Label ID="lblDate" runat="server" Text='<%# String.Format("{0:d}", DataBinder.Eval(Container.DataItem, "story_Date")) %>'></asp:Label>
The key here is the utilization of the String.Format() method. The method signature is:
String.Format(String, Object)
We simply provide first a parameter containing the string format and then the actual value we want to apply this formatting to.
For the String parameter, we provide:
"{0:d}"
The 0 acts as a sort of placeholder in which any following values (preceeded by a comma) will be substituted in order. For additional values we define more placeholder elements, ie "{1} ... {2}". (This structure is what is technically called a params array object.) We specify custom formatting by placing a semi-colon followed by a particular formatting character. In our case, we can simply place a :d, which formats the value placed in {0} as a short data (1/1/08) as opposed to the full data and timestamp formated defaulted to by ASP.NET.
The second parameter required, an Object, is simply our original databinder statement:
DataBinder.Eval(Container.DataItem, "story_Date")
The result of the DataBinder.Eval is substituted into the {0}, then formatted as a short data, eliminating the 12:00:00 AM appended by ASP.NET. This gives us the result we initially set out for - a typical data format without the annoying timestamp!
Moving Forward...
This is a common application of formatting a date string, however the possibilites and control you have are very broad and encompass any scenario you may encounter. The String.Format() method is very flexible, and we can utilize this to customize our formatting to any specification.
Common String.Format() functions you may require include:
| Date & Time Formatting Functions | | "{0:D}" | Long Date | July 1, 2008 | | "{0:d}" | Short Date | 7/1/2008 | | "{0:F}" | Full Date w/Long Time | July 1, 2008 1:45:15 PM | | "{0:f}" | Full Date w/Time | July 1, 2008 1:45 PM | | "{0:T}" | Long Time | 1:45:15 PM | | "{0:t}" | Short Time | 1:45 PM | | "{0:M}" | Month & Day | July 1 | | "{0:Y}" | Year & Month | July, 2008 |
While these built in .NET string formatting functions work very well, there are still times when the results just aren't exactly to your liking. To remedy this, String.Format() also supports custom data formatting features that allow you to build your own formats tuned to the exact format you require.
Date formatting specifiers for custom output include:
| Day Specifiers | | "{0:dd}" | Numerical Day | July 1, 2008 -> 1) | | "{0:ddd}" | Common Abbreviated Day Name | July 1, 2008 -> Tue | | "{0:dddd}" | Common Full Day Name | July 1, 2008 -> Tuesday | | Month & Year Specifiers | | "{0:MM}" | Numerical Month | July -> 7 | | "{0:MMM}" | Common Abbreviated Month | January -> Jan | | "{0:MMMM}" | Common Full Month | January -> January | | "{0:yy}" | Two Digit Year | 2008 -> 08 | | "{0:yyyy}" | Full Four Digit Year | 2008 -> 2008 | | "{0:gg}" | Era (A.D., B.C.) | | Time Specifiers | | "{0:hh}" | 2 Digit Hour, Standard Format | 1:00 -> 1 | | "{0:HH}" | 2 Digit Hour, Military Format | 1:00 -> 13 | | "{0:mm}" | 2 Digit Minute | 1:15 -> 15 | | "{0:ss}" | 2 Digit Seconds | 1:15:30 -> 30 | | Format Specifiers | | : | Colon Seperator | {0:hh:mm:gg} | | / | Forward Slash Seperator | {0:MM/yy} |
As you can see, virtually any formatting can be accomplished using these built-in string formatting functions.
Content - C#, VB, .NET, Strings, Formatting
|
|
|
|
|
|