/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/runtime-libraries/snippets/System.Globalization/DateTimeFormatInfo/csharp/formatprovider1.cs
43 строки
2 KB
Genevieve Warren
Extract remarks for System.Globalization* (#38912)
29 дек 2023, 20:03
Не верифицирован
29 дек 2023, 20:03
7f945f9
Код
Авторство
О чём код?
// <Snippet9> using System; using System.Globalization; public class CurrentCultureFormatProvider : IFormatProvider { public Object GetFormat(Type formatType) { Console.WriteLine("Requesting an object of type {0}", formatType.Name); if (formatType == typeof(NumberFormatInfo)) return NumberFormatInfo.CurrentInfo; else if (formatType == typeof(DateTimeFormatInfo)) return DateTimeFormatInfo.CurrentInfo; else return null; } } public class FormatProviderEx1 { public static void Main() { DateTime dateValue = new DateTime(2013, 5, 28, 13, 30, 0); string value = dateValue.ToString("F", new CurrentCultureFormatProvider()); Console.WriteLine(value); Console.WriteLine(); string composite = String.Format(new CurrentCultureFormatProvider(), "Date: {0:d} Amount: {1:C} Description: {2}", dateValue, 1264.03m, "Service Charge"); Console.WriteLine(composite); Console.WriteLine(); } } // The example displays output like the following: // Requesting an object of type DateTimeFormatInfo // Tuesday, May 28, 2013 1:30:00 PM // // Requesting an object of type ICustomFormatter // Requesting an object of type DateTimeFormatInfo // Requesting an object of type NumberFormatInfo // Date: 5/28/2013 Amount: $1,264.03 Description: Service Charge // </Snippet9>