Tuesday, May 4, 2021

C# DateTimeFormatInfo, NumberFormatInfo and RegionInfo

EXAMPLE 1
using System;
using System.Globalization; // to use CultureInfo class
namespace ConsoleCultureInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            //get current culture info and its properties
            CultureInfo currentCi = CultureInfo.CurrentCulture;
            DateTimeFormatInfo dtfi = currentCi.DateTimeFormat;
            //Calendar clndr = dtfi.Calendar;
//DateTimeFormatInfo object returns all kind of information about date and time formats/pattern
            Console.WriteLine("DateSeparator: {0}", dtfi.DateSeparator);
            Console.WriteLine("TimeSeparator: {0}", dtfi.TimeSeparator);
            Console.WriteLine("FullDateTimePattern: {0}", dtfi.FullDateTimePattern);
            Console.WriteLine("ShortDatePattern: {0}", dtfi.ShortDatePattern);
            Console.WriteLine("ShortTimePattern: {0}", dtfi.ShortTimePattern);
            Console.WriteLine("YearMonthPattern: {0}", dtfi.YearMonthPattern);
            Console.WriteLine("FirstDayOfWeek: {0}", dtfi.FirstDayOfWeek);
            Console.WriteLine("DayNames.Length: {0}", dtfi.DayNames.Length);
            foreach (var DayName in dtfi.DayNames)
            {
                Console.WriteLine(DayName);
            }
           
            Console.ReadKey();
        }
    }
}
OUTPUT
DateSeparator: -
TimeSeparator: :
FullDateTimePattern: MMMM d, yyyy h:mm:ss tt
ShortDatePattern: yyyy-MM-dd
ShortTimePattern: h:mm tt
YearMonthPattern: MMMM, yyyy
FirstDayOfWeek: Sunday
DayNames.Length: 7
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

EXAMPLE 2
using System;
using System.Globalization;

namespace ConsoleNumberFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            CultureInfo ci = CultureInfo.CurrentCulture;
            NumberFormatInfo nfi = ci.NumberFormat;
            Console.WriteLine(nfi.CurrencyDecimalDigits);
            Console.WriteLine(nfi.CurrencySymbol);
            Console.WriteLine(nfi.PercentSymbol);
            Console.WriteLine(nfi.NativeDigits.Length);
            foreach (var ndigit in nfi.NativeDigits)
            {
                Console.Write("{0} ",ndigit);
            }
            Console.ReadKey();
        }
        
    }
}
OUTPUT
2
$
%
10
0 1 2 3 4 5 6 7 8 9

EXAMPLE 3

using System;
using System.Globalization;

namespace ConsoleRegionInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            RegionInfo currentRegion = new RegionInfo(CultureInfo.CurrentCulture.Name);
            // Name is 2 letters language - 2 letters region e.g. en-US
            Console.WriteLine(currentRegion.Name);
            // DisplayName is full name of region as per localized .NET framework
            Console.WriteLine(currentRegion.DisplayName);
            // EnglishName is full name of region in English language
            Console.WriteLine(currentRegion.EnglishName);
            // NativeName is full name of region in that region's language
            Console.WriteLine(currentRegion.NativeName);
            Console.WriteLine(currentRegion.CurrencyEnglishName);
            Console.WriteLine(currentRegion.CurrencyNativeName);
            Console.WriteLine(currentRegion.CurrencySymbol);
            Console.WriteLine(currentRegion.TwoLetterISORegionName);
            Console.ReadKey();
        }
    }
}
OUTPUT
en-CA
Canada
Canada
Canada
Canadian Dollar
Canadian Dollar
$
CA

No comments:

Post a Comment

Hot Topics