{
HttpContext c = HttpContext.Current;
#region String Format for Double
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
// max. two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567); // "123.5"
String.Format("{0:00.0}", 23.4567); // "23.5"
String.Format("{0:00.0}", 3.4567); // "03.5"
String.Format("{0:00.0}", -3.4567); // "-03.5"
String.Format("{0:0,0.0}", 12345.67); // "12,345.7"
String.Format("{0:0,0}", 12345.67); // "12,346"
String.Format("{0:0.0}", 0.0); // "0.0"
String.Format("{0:0.#}", 0.0); // "0"
String.Format("{0:#.0}", 0.0); // ".0"
String.Format("{0:#.#}", 0.0); // ""
String.Format("{0,10:0.0}", 123.4567); // " 123.5"
String.Format("{0,-10:0.0}", 123.4567); // "123.5 "
String.Format("{0,10:0.0}", -123.4567); // " -123.5"
String.Format("{0,-10:0.0}", -123.4567); // "-123.5 "
String.Format("{0:0.00;minus 0.00;zero}", 123.4567); // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567); // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0); // "zero"
String.Format("{0:my number is 0.0}", 12.3); // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3); // "12aaa.bbb3"
double value;
value = 123;
c.Response.Write(value.ToString("00000"));
// Displays 00123
value = 1.2;
c.Response.Write(value.ToString("0.00", CultureInfo.InvariantCulture));
// Displays 1.20
c.Response.Write(value.ToString("00.00", CultureInfo.InvariantCulture));
// Displays 01.20
c.Response.Write(value.ToString("00.00",
CultureInfo.CreateSpecificCulture("da-DK")));
// Displays 01,20
value = .56;
c.Response.Write(value.ToString("0.0", CultureInfo.InvariantCulture));
// Displays 0.6
value = 1234567890;
c.Response.Write(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
c.Response.Write(value.ToString("#,#",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 1.234.567.890
value = 1.2;
c.Response.Write(value.ToString("#.##", CultureInfo.InvariantCulture));
// Displays 1.2
value = 123;
c.Response.Write(value.ToString("#####"));
// Displays 123
value = 123456;
c.Response.Write(value.ToString("[##-##-##]"));
// Displays [12-34-56]
value = 1234567890;
c.Response.Write(value.ToString("#"));
// Displays 1234567890
c.Response.Write(value.ToString("(###) ###-####"));
// Displays (123) 456-7890
value = 1.2;
c.Response.Write(value.ToString("0.00", CultureInfo.InvariantCulture));
// Displays 1.20
c.Response.Write(value.ToString("00.00", CultureInfo.InvariantCulture));
// Displays 01.20
c.Response.Write(value.ToString("00.00",
CultureInfo.CreateSpecificCulture("da-DK")));
// Displays 01,20
value = .086;
c.Response.Write(value.ToString("#0.##%", CultureInfo.InvariantCulture));
// Displays 8.6%
value = 86000;
c.Response.Write(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
// Displays 8.6E+4
value = 1234567890;
c.Response.Write(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
c.Response.Write(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235
value = 1234567890;
c.Response.Write(value.ToString("#,,", CultureInfo.InvariantCulture));
// Displays 1235
c.Response.Write(value.ToString("#,,,", CultureInfo.InvariantCulture));
// Displays 1
c.Response.Write(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235
value = .086;
c.Response.Write(value.ToString("#0.##%", CultureInfo.InvariantCulture));
// Displays 8.6%
value = .00354;
string perMilleFmt = "#0.## " + '\u2030';
c.Response.Write(value.ToString(perMilleFmt, CultureInfo.InvariantCulture));
// Displays 3.54‰
value = 86000;
c.Response.Write(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
// Displays 8.6E+4
c.Response.Write(value.ToString("0.###E+000", CultureInfo.InvariantCulture));
// Displays 8.6E+004
c.Response.Write(value.ToString("0.###E-000", CultureInfo.InvariantCulture));
// Displays 8.6E004
//int value = 123;
value = 123;
c.Response.Write(value.ToString("\\#\\#\\# ##0 dollars and \\0\\0 cents \\#\\#\\#"));
// Displays ### 123 dollars and 00 cents ###
c.Response.Write(value.ToString(@"\#\#\# ##0 dollars and \0\0 cents \#\#\#"));
// Displays ### 123 dollars and 00 cents ###
c.Response.Write(value.ToString("\\\\\\\\\\\\ ##0 dollars and \\0\\0 cents \\\\\\\\\\\\"));
// Displays \\\ 123 dollars and 00 cents \\\
c.Response.Write(value.ToString(@"\\\\\\ ##0 dollars and \0\0 cents \\\\\\"));
// Displays \\\ 123 dollars and 00 cents \\\
double posValue = 1234;
double negValue = -1234;
string fmt1 = "##;(##)";
c.Response.Write(posValue.ToString(fmt1)); // Displays 1234
c.Response.Write(negValue.ToString(fmt1)); // Displays (1234)
double number1 = 1234567890;
string value1 = number1.ToString("(###) ###-####");
c.Response.Write(value1);
int number2 = 42;
string value2 = number2.ToString("My Number = #");
c.Response.Write(value2);
// The example displays the following output:
// (123) 456-7890
// My Number = 42
#endregion
#region String Format for Int
String.Format("{0:00000}", 15); // "00015"
String.Format("{0:00000}", -15); // "-00015"
String.Format("{0,5}", 15); // " 15"
String.Format("{0,-5}", 15); // "15 "
String.Format("{0,5:000}", 15); // " 015"
String.Format("{0,-5:000}", 15); // "015 "
String.Format("{0:#;minus #}", 15); // "15"
String.Format("{0:#;minus #}", -15); // "minus 15"
String.Format("{0:#;minus #;zero}", 0); // "zero"
String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456"
String.Format("{0:##-####-####}", 8958712551); // "89-5871-2551"
#endregion
#region String Format for DateTime
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)
// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt); // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008"
// two/four digit year
String.Format("{0:MM/dd/yy}", dt); // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
String.Format("{0:t}", dt); // "4:05 PM" ShortTime
String.Format("{0:d}", dt); // "3/9/2008" ShortDate
String.Format("{0:T}", dt); // "4:05:07 PM" LongTime
String.Format("{0:D}", dt); // "Sunday, March 09, 2008" LongDate
String.Format("{0:f}", dt); // "Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime
String.Format("{0:F}", dt); // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt); // "3/9/2008 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt); // "3/9/2008 4:05:07 PM" ShortDate+LongTime
String.Format("{0:m}", dt); // "March 09" MonthDay
String.Format("{0:y}", dt); // "March, 2008" YearMonth
String.Format("{0:r}", dt); // "Sun, 09 Mar 2008 16:05:07 GMT" RFC1123
String.Format("{0:s}", dt); // "2008-03-09T16:05:07" SortableDateTime
String.Format("{0:u}", dt); // "2008-03-09 16:05:07Z" UniversalSortableDateTime
#endregion
#region IFormatProvider for Numbers
// format float to string
float num = 1.5f;
string str = num.ToString(CultureInfo.InvariantCulture.NumberFormat); // "1.5"
string str2 = num.ToString(CultureInfo.GetCultureInfo("de-DE").NumberFormat); // "1,5"
// parse float from string
float num3 = float.Parse("1.5", CultureInfo.InvariantCulture.NumberFormat);
float num2 = float.Parse("1,5", CultureInfo.GetCultureInfo("de-DE").NumberFormat);
#endregion
#region Culture Names
// get culture names
List
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
string specName = "(none)";
try
{
specName = CultureInfo.CreateSpecificCulture(ci.Name).Name;
}
catch
{
}
list.Add(String.Format("{0,-12}{1,-12}{2}", ci.Name, specName, ci.EnglishName));
}
list.Sort(); // sort by name
// write to console
HttpContext.Current.Response.Write("CULTURE SPEC.CULTURE ENGLISH NAME");
HttpContext.Current.Response.Write("--------------------------------------------------------------");
foreach (string q in list)
HttpContext.Current.Response.Write(q);
#endregion
decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
#region Numbers2
int pos = 10;
int neg = -10;
int bigpos = 123456;
int bigneg = -123456;
int zero = 0;
string strInt = "120ab";
String.Format("{0:00000}", pos); //"00010"
String.Format("{0:00000}", neg); //"-00010"
String.Format("{0:00000}", bigpos); //"123456"
String.Format("{0:00000}", bigneg); //"-123456"
String.Format("{0:00000}", zero); //"00000"
String.Format("{0:00000}", strInt); //"120ab"
String.Format("{0:#####}", pos); //"10"
String.Format("{0:#####}", neg); //"-10"
String.Format("{0:#####}", bigpos); //"123456"
String.Format("{0:#####}", bigneg); //"-123456"
String.Format("{0:#####}", zero); //""
String.Format("{0:#####}", strInt); //"120ab"
//While playing around with this, I made an interesting observation:
String.Format("{0:X00000}", pos); //"A"
String.Format("{0:X00000}", neg); //"FFFFFFF6"
String.Format("{0:X#####}", pos); //"X10"
String.Format("{0:X#####}", neg); //"-X10"
// zeros (optional): It actually
//has a different meaning depending on which numeric specifier you use.
// C or c (Currency): It represent how many decimal place of zeros to show.
String.Format("{0:C4}", pos); //"$10.0000"
String.Format("{0:C4}", neg); //"($10.0000)"
// D or d (Decimal): It represent leading zeros
String.Format("{0:D4}", pos); //"0010"
String.Format("{0:D4}", neg); //"-0010"
// E or e (Exponential): It represent how many decimal places of zeros to show.
String.Format("{0:E4}", pos); //"1.0000E+001"
String.Format("{0:E4}", neg); //"-1.0000E+001"
// F or f (Fixed-point): It represent how many decimal places of zeros to show.
String.Format("{0:F4}", pos); //"10.0000"
String.Format("{0:F4}", neg); //"-10.0000"
// G or g (General): This does nothing
String.Format("{0:G4}", pos); //"10"
String.Format("{0:G4}", neg); //"-10"
// N or n (Number): It represent how many decimal places of zeros to show.
String.Format("{0:N4}", pos); //"10.0000"
String.Format("{0:N4}", neg); //"-10.0000"
// P or p (Percent): It represent how many decimal places of zeros to show.
String.Format("{0:P4}", pos); //"1,000.0000%"
String.Format("{0:P4}", neg); //"-1,000.0000%"
// R or r (Round-Trip): This is invalid, FormatException is thrown.
String.Format("{0:R4}", pos); //FormatException thrown
String.Format("{0:R4}", neg); //FormatException thrown
// X or x (Hex): It represent leading zeros
String.Format("{0:X4}", pos); //"000A"
String.Format("{0:X4}", neg); //"FFFFFFF6"
// nothing: This is invalid, no exception is thrown.
String.Format("{0:4}", pos); //"4"
String.Format("{0:4}", neg); //"-4"
String.Format("{0:(###) ###-####}", 18005551212); //Produces (1800) 555-1212 and not (800) 555-1212.
#endregion
#region NumericFormats
// Display string representations of numbers for en-us culture
CultureInfo cit = new CultureInfo("en-us");
// Output floating point values
double floating = 10761.937554;
c.Response.Write(string.Format("C: {0}",
floating.ToString("C", cit))); // Displays "C: $10,761.94"
c.Response.Write(string.Format("E: {0}",
floating.ToString("E03", cit))); // Displays "E: 1.076E+004"
c.Response.Write(string.Format("F: {0}",
floating.ToString("F04", cit))); // Displays "F: 10761.9376"
c.Response.Write(string.Format("G: {0}",
floating.ToString("G", cit))); // Displays "G: 10761.937554"
c.Response.Write(string.Format("N: {0}",
floating.ToString("N03", cit))); // Displays "N: 10,761.938"
c.Response.Write(string.Format("P: {0}",
(floating / 10000).ToString("P02", cit))); // Displays "P: 107.62 %"
c.Response.Write(string.Format("R: {0}",
floating.ToString("R", cit))); // Displays "R: 10761.937554"
c.Response.Write("");
// Output integral values
int integral = 8395;
c.Response.Write(string.Format("C: {0}",
integral.ToString("C", cit))); // Displays "C: $8,395.00"
c.Response.Write(string.Format("D: {0}",
integral.ToString("D6", cit))); // Displays D: 008395""
c.Response.Write(string.Format("E: {0}",
integral.ToString("E03", cit))); // Displays "E: 8.395E+003"
c.Response.Write(string.Format("F: {0}",
integral.ToString("F01", cit))); // Displays "F: 8395.0"
c.Response.Write(string.Format("G: {0}",
integral.ToString("G", cit))); // Displays "G: 8395"
c.Response.Write(string.Format("N: {0}",
integral.ToString("N01", cit))); // Displays "N: 8,395.0"
c.Response.Write(string.Format("P: {0}",
(integral / 10000).ToString("P02", cit))); // Displays "P: 83.95 %"
c.Response.Write(string.Format("X: 0x{0}",
integral.ToString("X", cit))); // Displays "X: 0x20CB"
c.Response.Write("");
#endregion
#region Standard Date and Time Format Strings
// Display using current (en-us) culture's short date format
DateTime thisDate = new DateTime(2008, 3, 15);
c.Response.Write(thisDate.ToString("d")); // Displays 3/15/2008
// Display using pt-BR culture's short date format
//DateTime thisDate = new DateTime(2008, 3, 15);
CultureInfo culture = new CultureInfo("pt-BR");
c.Response.Write(thisDate.ToString("d", culture)); // Displays 15/3/2008
// Display using date format information from hr-HR culture
//DateTime thisDate = new DateTime(2008, 3, 15);
DateTimeFormatInfo fmt = (new CultureInfo("hr-HR")).DateTimeFormat;
c.Response.Write(thisDate.ToString("d", fmt)); // Displays 15.3.2008
DateTime date1 = new DateTime(2008, 4, 10);
c.Response.Write(date1.ToString("d", DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008
c.Response.Write(date1.ToString("d",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays 4/10/2008
c.Response.Write(date1.ToString("d",
CultureInfo.CreateSpecificCulture("en-NZ")));
// Displays 10/04/2008
c.Response.Write(date1.ToString("d",
CultureInfo.CreateSpecificCulture("de-DE")));
// Displays 10.04.2008
DateTime datez1 = new DateTime(2008, 4, 10);
c.Response.Write(datez1.ToString("D",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008
c.Response.Write(datez1.ToString("D",
CultureInfo.CreateSpecificCulture("pt-BR")));
// Displays quinta-feira, 10 de abril de 2008
c.Response.Write(datez1.ToString("D",
CultureInfo.CreateSpecificCulture("es-MX")));
// Displays jueves, 10 de abril de 2008
DateTime date2 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date2.ToString("f",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 6:30 AM
c.Response.Write(date2.ToString("f",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays jeudi 10 avril 2008 06:30
DateTime date3 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date3.ToString("F",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 6:30:00 AM
c.Response.Write(date3.ToString("F",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays jeudi 10 avril 2008 06:30:00
DateTime date4 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date4.ToString("g",
DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008 06:30
c.Response.Write(date4.ToString("g",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30 AM
c.Response.Write(date4.ToString("g",
CultureInfo.CreateSpecificCulture("fr-BE")));
// Displays10/04/2008 6:30
DateTime date5 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date5.ToString("G",
DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008 06:30:00
c.Response.Write(date5.ToString("G",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30:00 AM
c.Response.Write(date5.ToString("G",
CultureInfo.CreateSpecificCulture("nl-BE")));
// Displays 10/04/2008 6:30:00
DateTime date6 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date6.ToString("m",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays April 10
c.Response.Write(date6.ToString("m",
CultureInfo.CreateSpecificCulture("ms-MY")));
// Displays 10 April
DateTime date7 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset = new DateTimeOffset(date7,
TimeZoneInfo.Local.GetUtcOffset(date1));
c.Response.Write(date7.ToString("o"));
// Displays 2008-04-10T06:30:00.0000000
c.Response.Write(dateOffset.ToString("o"));
// Displays 2008-04-10T06:30:00.0000000-07:00
// Round-trip DateTime values.
DateTime originalDate, newDate;
string dateString;
// Round-trip a local time.
originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 10, 6, 30, 0), DateTimeKind.Local);
dateString = originalDate.ToString("o");
newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);
c.Response.Write(string.Format( "Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind,
newDate, newDate.Kind));
// Round-trip a UTC time.
originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 12, 9, 30, 0), DateTimeKind.Utc);
dateString = originalDate.ToString("o");
newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);
c.Response.Write(string.Format( "Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind,
newDate, newDate.Kind));
// Round-trip time in an unspecified time zone.
originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 13, 12, 30, 0), DateTimeKind.Unspecified);
dateString = originalDate.ToString("o");
newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);
c.Response.Write(string.Format( "Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind,
newDate, newDate.Kind));
// Round-trip a DateTimeOffset value.
DateTimeOffset originalDTO = new DateTimeOffset(2008, 4, 12, 9, 30, 0, new TimeSpan(-8, 0, 0));
dateString = originalDTO.ToString("o");
DateTimeOffset newDTO = DateTimeOffset.Parse(dateString, null, DateTimeStyles.RoundtripKind);
c.Response.Write(string.Format( "Round-tripped {0} to {1}.", originalDTO, newDTO));
// The example displays the following output:
// Round-tripped 4/10/2008 6:30:00 AM Local to 4/10/2008 6:30:00 AM Local.
// Round-tripped 4/12/2008 9:30:00 AM Utc to 4/12/2008 9:30:00 AM Utc.
// Round-tripped 4/13/2008 12:30:00 PM Unspecified to 4/13/2008 12:30:00 PM Unspecified.
// Round-tripped 4/12/2008 9:30:00 AM -08:00 to 4/12/2008 9:30:00 AM -08:00.
DateTime date8 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset2 = new DateTimeOffset(date8,
TimeZoneInfo.Local.GetUtcOffset(date1));
c.Response.Write(date8.ToUniversalTime().ToString("r"));
// Displays Thu, 10 Apr 2008 13:30:00 GMT
c.Response.Write(dateOffset2.ToUniversalTime().ToString("r"));
// Displays Thu, 10 Apr 2008 13:30:00 GMT
DateTime date11 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date11.ToString("s"));
// Displays 2008-04-10T06:30:00
DateTime date12 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date12.ToString("t",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 6:30 AM
c.Response.Write(date12.ToString("t",
CultureInfo.CreateSpecificCulture("es-ES")));
// Displays 6:30
DateTime date13 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date13.ToString("T",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 6:30:00 AM
c.Response.Write(date13.ToString("T",
CultureInfo.CreateSpecificCulture("es-ES")));
// Displays 6:30:00
DateTime date14 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date14.ToUniversalTime().ToString("u"));
// Displays 2008-04-10 13:30:00Z
DateTime date15 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date15.ToString("U",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 1:30:00 PM
c.Response.Write(date15.ToString("U",
CultureInfo.CreateSpecificCulture("sv-FI")));
// Displays den 10 april 2008 13:30:00
DateTime date16 = new DateTime(2008, 4, 10, 6, 30, 0);
c.Response.Write(date16.ToString("Y",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays April, 2008
c.Response.Write(date16.ToString("y",
CultureInfo.CreateSpecificCulture("af-ZA")));
// Displays April 2008
#endregion
#region Custom Date and Time Format Strings
DateTime dat1 = new DateTime(2008, 8, 29, 19, 27, 15);
c.Response.Write(dat1.ToString("d, M",
CultureInfo.InvariantCulture));
// Displays 29, 8
c.Response.Write(dat1.ToString("d MMMM",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays 29 August
c.Response.Write(dat1.ToString("d MMMM",
CultureInfo.CreateSpecificCulture("es-MX")));
// Displays 29 agosto
DateTime dat2 = new DateTime(2008, 1, 2, 6, 30, 15);
c.Response.Write(dat2.ToString("dd, MM",
CultureInfo.InvariantCulture));
// 02, 01
DateTime dat3 = new DateTime(2008, 8, 29, 19, 27, 15);
c.Response.Write(dat3.ToString("ddd d MMM",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Fri 29 Aug
c.Response.Write(dat3.ToString("ddd d MMM",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays ven. 29 ao�t
DateTime dat4 = new DateTime(2008, 8, 29, 19, 27, 15);
c.Response.Write(dat4.ToString("dddd dd MMMM",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Friday 29 August
c.Response.Write(dat4.ToString("dddd dd MMMM",
CultureInfo.CreateSpecificCulture("it-IT")));
// Displays venerd� 29 agosto
DateTime dat5 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo c2 = CultureInfo.InvariantCulture;
c.Response.Write(dat5.ToString("hh:mm:ss.f", c2));
// Displays 07:27:15.0
c.Response.Write(dat5.ToString("hh:mm:ss.F", c2));
// Displays 07:27:15
c.Response.Write(dat5.ToString("hh:mm:ss.ff", c2));
// Displays 07:27:15.01
c.Response.Write(dat5.ToString("hh:mm:ss.FF", c2));
// Displays 07:27:15.01
c.Response.Write(dat5.ToString("hh:mm:ss.fff", c2));
// Displays 07:27:15.018
c.Response.Write(dat5.ToString("hh:mm:ss.FFF", c2));
// Displays 07:27:15.018
DateTime da1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
c.Response.Write(da1.ToString("hh:mm:ss.f", c2));
// Displays 07:27:15.0
c.Response.Write(da1.ToString("hh:mm:ss.F", c2));
// Displays 07:27:15
c.Response.Write(da1.ToString("hh:mm:ss.ff", c2));
// Displays 07:27:15.01
c.Response.Write(da1.ToString("hh:mm:ss.FF", c2));
// Displays 07:27:15.01
c.Response.Write(da1.ToString("hh:mm:ss.fff", c2));
// Displays 07:27:15.018
c.Response.Write(da1.ToString("hh:mm:ss.FFF", c2));
// Displays 07:27:15.018
DateTime da2 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
c.Response.Write(da2.ToString("hh:mm:ss.f", c2));
// Displays 07:27:15.0
c.Response.Write(da2.ToString("hh:mm:ss.F", c2));
// Displays 07:27:15
c.Response.Write(da2.ToString("hh:mm:ss.ff", c2));
// Displays 07:27:15.01
c.Response.Write(da2.ToString("hh:mm:ss.FF", c2));
// Displays 07:27:15.01
c.Response.Write(da2.ToString("hh:mm:ss.fff", c2));
// Displays 07:27:15.018
c.Response.Write(da2.ToString("hh:mm:ss.FFF", c2));
// Displays 07:27:15.018
DateTime dateq = new DateTime(70, 08, 04);
c.Response.Write(dateq.ToString("MM/dd/yyyy g",
CultureInfo.InvariantCulture));
// Displays 08/04/0070 A.D.
c.Response.Write(dateq.ToString("MM/dd/yyyy g",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays 08/04/0070 ap. J.-C.
DateTime d1;
d1 = new DateTime(2008, 1, 1, 18, 9, 1);
c.Response.Write(d1.ToString("h:m:s.F t",
CultureInfo.InvariantCulture));
// Displays 6:9:1 P
c.Response.Write(d1.ToString("h:m:s.F t",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 �
d1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
c.Response.Write(d1.ToString("h:m:s.F t",
CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
c.Response.Write(d1.ToString("h:m:s.F t",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 �
DateTime d2;
d2 = new DateTime(2008, 1, 1, 18, 9, 1);
c.Response.Write(d2.ToString("hh:mm:ss tt",
CultureInfo.InvariantCulture));
// Displays 06:09:01 PM
c.Response.Write(d2.ToString("hh:mm:ss tt",
CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.
d2 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
c.Response.Write(d2.ToString("hh:mm:ss.ff tt",
CultureInfo.InvariantCulture));
// Displays 06:09:01.50 PM
c.Response.Write(d2.ToString("hh:mm:ss.ff tt",
CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01.50 du.
DateTime d12 = new DateTime(2008, 1, 1, 6, 9, 1);
c.Response.Write(d12.ToString("H:mm:ss",
CultureInfo.InvariantCulture));
// Displays 6:09:01
DateTime d21 = new DateTime(2008, 1, 1, 6, 9, 1);
c.Response.Write(d21.ToString("HH:mm:ss",
CultureInfo.InvariantCulture));
// Displays 06:09:01
c.Response.Write(DateTime.Now.ToString("%K"));
// Displays -07:00
c.Response.Write(DateTime.UtcNow.ToString("%K"));
// Displays Z
c.Response.Write(string.Format("'{0}'",
DateTime.SpecifyKind(DateTime.Now,
DateTimeKind.Unspecified).ToString("%K")));
// Displays ''
c.Response.Write(DateTimeOffset.Now.ToString("%K"));
// Displays -07:00
c.Response.Write(DateTimeOffset.UtcNow.ToString("%K"));
// Displays +00:00
c.Response.Write(new DateTimeOffset(2008, 5, 1, 6, 30, 0,
new TimeSpan(5, 0, 0)).ToString("%K"));
// Displays +05:00
DateTime d6;
d6 = new DateTime(2008, 1, 1, 18, 9, 1);
c.Response.Write(d6.ToString("h:m:s.F t",
CultureInfo.InvariantCulture));
// Displays 6:9:1 P
c.Response.Write(d6.ToString("h:m:s.F t",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 �
d6 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
c.Response.Write(d6.ToString("h:m:s.F t",
CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
c.Response.Write(d6.ToString("h:m:s.F t",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 �
DateTime d16;
d16 = new DateTime(2008, 1, 1, 18, 9, 1);
c.Response.Write(d16.ToString("hh:mm:ss tt",
CultureInfo.InvariantCulture));
// Displays 06:09:01 PM
c.Response.Write(d16.ToString("hh:mm:ss tt",
CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.
d16 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
c.Response.Write(d16.ToString("hh:mm:ss.ff tt",
CultureInfo.InvariantCulture));
// Displays 06:09:01.50 PM
c.Response.Write(d16.ToString("hh:mm:ss.ff tt",
CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01.50 du.
DateTime da1q = new DateTime(2008, 8, 18);
c.Response.Write(da1q.ToString("(M) MMM, MMMM",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays (8) Aug, August
c.Response.Write(da1q.ToString("(M) MMM, MMMM",
CultureInfo.CreateSpecificCulture("nl-NL")));
// Displays (8) aug, augustus
c.Response.Write(da1q.ToString("(M) MMM, MMMM",
CultureInfo.CreateSpecificCulture("lv-LV")));
// Displays (8) Aug, augusts
DateTime d0 = new DateTime(2008, 1, 2, 6, 30, 15);
c.Response.Write(d0.ToString("dd, MM",
CultureInfo.InvariantCulture));
// 02, 01
DateTime ate1 = new DateTime(2008, 8, 29, 19, 27, 15);
c.Response.Write(ate1.ToString("ddd d MMM",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Fri 29 Aug
c.Response.Write(ate1.ToString("ddd d MMM",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays ven. 29 ao�t
DateTime te1 = new DateTime(2008, 8, 29, 19, 27, 15);
c.Response.Write(te1.ToString("dddd dd MMMM",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Friday 29 August
c.Response.Write(te1.ToString("dddd dd MMMM",
CultureInfo.CreateSpecificCulture("it-IT")));
// Displays venerd� 29 agosto
DateTime e1;
e1 = new DateTime(2008, 1, 1, 18, 9, 1);
c.Response.Write(e1.ToString("h:m:s.F t",
CultureInfo.InvariantCulture));
// Displays 6:9:1 P
c.Response.Write(e1.ToString("h:m:s.F t",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 �
e1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
c.Response.Write(e1.ToString("h:m:s.F t",
CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
c.Response.Write(e1.ToString("h:m:s.F t",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 �
DateTime t1;
t1 = new DateTime(2008, 1, 1, 18, 9, 1);
c.Response.Write(t1.ToString("hh:mm:ss tt",
CultureInfo.InvariantCulture));
// Displays 06:09:01 PM
c.Response.Write(t1.ToString("hh:mm:ss tt",
CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.
t1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
c.Response.Write(t1.ToString("hh:mm:ss.ff tt",
CultureInfo.InvariantCulture));
// Displays 06:09:01.50 PM
c.Response.Write(t1.ToString("hh:mm:ss.ff tt",
CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01.50 du.
DateTime de12;
de12 = new DateTime(2008, 1, 1, 18, 9, 1);
c.Response.Write(de12.ToString("h:m:s.F t",
CultureInfo.InvariantCulture));
// Displays 6:9:1 P
c.Response.Write(de12.ToString("h:m:s.F t",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 �
de12 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
c.Response.Write(de12.ToString("h:m:s.F t",
CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
c.Response.Write(de12.ToString("h:m:s.F t",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 �
DateTime d213;
d213 = new DateTime(2008, 1, 1, 18, 9, 1);
c.Response.Write(d213.ToString("hh:mm:ss tt",
CultureInfo.InvariantCulture));
// Displays 06:09:01 PM
c.Response.Write(d213.ToString("hh:mm:ss tt",
CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.
d213 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
c.Response.Write(d213.ToString("hh:mm:ss.ff tt",
CultureInfo.InvariantCulture));
// Displays 06:09:01.50 PM
c.Response.Write(d213.ToString("hh:mm:ss.ff tt",
CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01.50 du.
DateTime date1s = new DateTime(1, 12, 1);
DateTime date2d = new DateTime(2010, 1, 1);
c.Response.Write(date1s.ToString("%y"));
// Displays 1
c.Response.Write(date1s.ToString("yy"));
// Displays 01
c.Response.Write(date1s.ToString("yyy"));
// Displays 001
c.Response.Write(date1s.ToString("yyyy"));
// Displays 0001
c.Response.Write(date1s.ToString("yyyyy"));
// Displays 00001
c.Response.Write(date2d.ToString("%y"));
// Displays 10
c.Response.Write(date2d.ToString("yy"));
// Displays 10
c.Response.Write(date2d.ToString("yyy"));
// Displays 2010
c.Response.Write(date2d.ToString("yyyy"));
// Displays 2010
c.Response.Write(date2d.ToString("yyyyy"));
// Displays 02010
DateTime date1g = new DateTime(1, 12, 1);
DateTime date2t = new DateTime(2010, 1, 1);
c.Response.Write(date1g.ToString("%y"));
// Displays 1
c.Response.Write(date1g.ToString("yy"));
// Displays 01
c.Response.Write(date1g.ToString("yyy"));
// Displays 001
c.Response.Write(date1g.ToString("yyyy"));
// Displays 0001
c.Response.Write(date1g.ToString("yyyyy"));
// Displays 00001
c.Response.Write(date2t.ToString("%y"));
// Displays 10
c.Response.Write(date2t.ToString("yy"));
// Displays 10
c.Response.Write(date2t.ToString("yyy"));
// Displays 2010
c.Response.Write(date2t.ToString("yyyy"));
// Displays 2010
c.Response.Write(date2t.ToString("yyyyy"));
// Displays 02010
DateTime date1o = new DateTime(1, 12, 1);
DateTime date2k = new DateTime(2010, 1, 1);
c.Response.Write(date1o.ToString("%y"));
// Displays 1
c.Response.Write(date1o.ToString("yy"));
// Displays 01
c.Response.Write(date1o.ToString("yyy"));
// Displays 001
c.Response.Write(date1o.ToString("yyyy"));
// Displays 0001
c.Response.Write(date1o.ToString("yyyyy"));
// Displays 00001
c.Response.Write(date2k.ToString("%y"));
// Displays 10
c.Response.Write(date2k.ToString("yy"));
// Displays 10
c.Response.Write(date2k.ToString("yyy"));
// Displays 2010
c.Response.Write(date2k.ToString("yyyy"));
// Displays 2010
c.Response.Write(date2k.ToString("yyyyy"));
// Displays 02010
#endregion
#region Composite Formatting
string myName = "Fred";
String.Format("Name = {0}, hours = {1:hh}", myName, DateTime.Now);
int value23 = 6324;
string output = string.Format("{0}{1:D}{2}",
"{", value23, "}");
c.Response.Write(output);
// The example displays the following output:
// {6324}
string FormatString1 = String.Format("{0:dddd MMMM}", DateTime.Now);
string FormatString2 = DateTime.Now.ToString("dddd MMMM");
int MyInt = 100;
c.Response.Write(string.Format("{0:C}", MyInt));
// The example displays the following output
// if en-US is the current culture:
// $100.00
c.Response.Write(String.Format("Name = {0}, hours = {1:hh}, minutes = {1:mm}",
myName, DateTime.Now));
// Depending on the current time, the example displays output like the following:
// Name = Fred, hours = 11, minutes = 30
string myFName = "Fred";
string myLName = "Opals";
int myInt = 100;
string FormatFName = String.Format("First Name = |{0,10}|", myFName);
string FormatLName = String.Format("Last Name = |{0,10}|", myLName);
string FormatPrice = String.Format("Price = |{0,10:C}|", myInt);
c.Response.Write(FormatFName);
c.Response.Write(FormatLName);
c.Response.Write(FormatPrice);
c.Response.Write("
");
FormatFName = String.Format("First Name = |{0,-10}|", myFName);
FormatLName = String.Format("Last Name = |{0,-10}|", myLName);
FormatPrice = String.Format("Price = |{0,-10:C}|", myInt);
c.Response.Write(FormatFName);
c.Response.Write(FormatLName);
c.Response.Write(FormatPrice);
// The example displays the following output on a system whose current
// culture is en-US:
// First Name = | Fred|
// Last Name = | Opals|
// Price = | $100.00|
//
// First Name = |Fred |
// Last Name = |Opals |
// Price = |$100.00 |
string myAddr = "Home";
FormatFName = String.Format("{0,-15} |{1,10}|", "First name:", myFName);
FormatLName = String.Format("{0,-15} |{1,10}|", "Last name:", myLName);
string FormatAddr = String.Format("{0,-15} |{1,10}|", "Home address:", myAddr);
FormatPrice = String.Format("{0,-15} |{1,10:C}|", "Price:", myInt);
c.Response.Write(FormatFName);
c.Response.Write(FormatLName);
c.Response.Write(FormatAddr);
c.Response.Write(FormatPrice);
//This will output:
//First name: | Fred|
//Last name: | Opals|
//Home address: | Home|
//Price: | $100.00|
#endregion
}
Hiç yorum yok:
Yorum Gönder