System.Globalization Namespace DateTimeFormatInfo Class
Returns the standard patterns in which DateTime values can be formatted.
1. Returns all the standard patterns in which DateTime values can be formatted.
2. Returns all the standard patterns in which DateTime values can be formatted using the specified format character.
The following example uses both versions of GetAllDateTimePatterns to fetch and print the DateTime patterns for the current calendar.
<script language="C#" runat="server">
DateTime now;
string html;
public void Page_Load ( Object src, EventArgs e ) {
// Create a new DateTimeFormatinfo instance.
DateTimeFormatInfo myDtfi = new DateTimeFormatInfo ( );
// Create a new DateTime object
now = DateTime.Now;
// Get and print all the patterns.
String [ ] myPatterns = myDtfi.GetAllDateTimePatterns ( );
html += "<table align='center' class='data' width=80% cellspacing=1>";
html += "<tr><th colspan=3>All the patterns:</th></tr>";
printValues ( myPatterns );
// Get and print the patterns associated with some of the format characters.
myPatterns = myDtfi.GetAllDateTimePatterns ( 'd' );
html += "<table align='center' class='data' width=80% cellspacing=1>";
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'd' ) :</th></tr>";
printValues ( myPatterns );
myPatterns = myDtfi.GetAllDateTimePatterns ( 'D' );
html += "<table align='center' class='data' width=80% cellspacing=1>";
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'D' ) :</th></tr>";
printValues ( myPatterns );
myPatterns = myDtfi.GetAllDateTimePatterns ( 'f' );
html += "<table align='center' class='data' width=80% cellspacing=1>";
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'f' ) :</th></tr>";
printValues ( myPatterns );
myPatterns = myDtfi.GetAllDateTimePatterns ( 'F' );
html += "<table align='center' class='data' width=80% cellspacing=1>";
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'F' ) :</th></tr>";
printValues ( myPatterns );
myPatterns = myDtfi.GetAllDateTimePatterns ( 'r' );
html += "<table align='center' class='data' width=80% cellspacing=1>";
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'r' ) :</th></tr>";
printValues ( myPatterns );
myPatterns = myDtfi.GetAllDateTimePatterns ( 'R' );
html += "<table align='center' class='data' width=80% cellspacing=1>";
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'R' ) :</th></tr>";
printValues ( myPatterns );
}
void printValues ( String [ ] myArray ) {
int i = 0;
foreach ( String s in myArray ) {
html += "<td>" + i++ + "</td>";
html += "<td>" + s + "</td>";
html += "<td>" + now.ToString ( s ) + "</td></tr>";
}
html += "</table><br>";
}
</script>
<script language="VB" runat="server">
Dim now As DateTime
Dim html As String
Public Sub Page_Load ( sender As [Object], e As EventArgs )
' Create a new DateTimeFormatinfo instance.
Dim myDtfi As New DateTimeFormatInfo ( )
' Create a new DateTime object
now = DateTime.Now
' Get and print all the patterns.
Dim myPatterns As [String] ( ) = myDtfi.GetAllDateTimePatterns ( )
html += "<table align='center' class='data' width=80% cellspacing=1>"
html += "<tr><th colspan=3>All the patterns:</th></tr>"
printValues ( myPatterns )
' Get and print the patterns associated with some of the format characters.
myPatterns = myDtfi.GetAllDateTimePatterns ( 'd' )
html += "<table align='center' class='data' width=80% cellspacing=1>"
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'd' ) :</th></tr>"
printValues ( myPatterns )
myPatterns = myDtfi.GetAllDateTimePatterns ( 'D' )
html += "<table align='center' class='data' width=80% cellspacing=1>"
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'D' ) :</th></tr>"
printValues ( myPatterns )
myPatterns = myDtfi.GetAllDateTimePatterns ( 'f' )
html += "<table align='center' class='data' width=80% cellspacing=1>"
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'f' ) :</th></tr>"
printValues ( myPatterns )
myPatterns = myDtfi.GetAllDateTimePatterns ( 'F' )
html += "<table align='center' class='data' width=80% cellspacing=1>"
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'F' ) :</th></tr>"
printValues ( myPatterns )
myPatterns = myDtfi.GetAllDateTimePatterns ( 'r' )
html += "<table align='center' class='data' width=80% cellspacing=1>"
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'r' ) :</th></tr>"
printValues ( myPatterns )
myPatterns = myDtfi.GetAllDateTimePatterns ( 'R' )
html += "<table align='center' class='data' width=80% cellspacing=1>"
html += "<tr><th colspan=3>The patterns for DateTime.ToString ( 'R' ) :</th></tr>"
printValues ( myPatterns )
End Sub 'Page_Load
Sub printValues ( myArray ( ) As [String] )
Dim i As Integer = 0
Dim s As [String]
For Each s In myArray
html += "<td>" + i + 1 + "</td>"
html += "<td>" + s + "</td>"
html += "<td>" + now.ToString ( s ) + "</td></tr>"
Next s
html += "</table><br>"
End Sub 'printValues
</script> |
|
C# |
VB |
Show me
DateTimeFormatInfo Members