Csharp/C Sharp by API/System.Globalization/DateTimeFormatInfo

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

DateTimeFormatInfo.CurrentInfo

<source lang="csharp"> using System; using System.Globalization; class Class1 {

 static void Main(string[] args) {
        CultureInfo MyCulture = new CultureInfo("es-ES");
        DateTimeFormatInfo Myfmt;
        DateTimeFormatInfo fmt1;
        Myfmt = MyCulture.DateTimeFormat;
        fmt1  = DateTimeFormatInfo.CurrentInfo;
        Console.WriteLine(Myfmt.GetMonthName(12));
        Console.WriteLine(fmt1.GetMonthName(12));
     }

}


 </source>


DateTimeFormatInfo.DateSeparator

<source lang="csharp"> using System; using System.Globalization; using System.Threading; class Class1 {

 static void Main(string[] args) {
     CultureInfo MyCulture = new CultureInfo("fr-FR");
     DateTime MyDate = System.DateTime.Now;
     Thread.CurrentThread.CurrentCulture = MyCulture;
     DateTimeFormatInfo dtf = MyCulture.DateTimeFormat;
     //Change date and time separator
     dtf.DateSeparator = "\\";
     dtf.TimeSeparator = "&";
     Console.WriteLine();
     Console.WriteLine(MyDate.ToLongDateString());
     Console.WriteLine(MyDate.ToLongTimeString());
     Console.WriteLine(MyDate.ToShortDateString());
     Console.WriteLine(MyDate.ToShortTimeString());
  }

}


 </source>


DateTimeFormatInfo.DateTimeFormat

<source lang="csharp"> using System; using System.Globalization; class Class1 {

 static void Main(string[] args) {
        CultureInfo MyCulture = new CultureInfo("es-ES");
        DateTimeFormatInfo Myfmt;
        DateTimeFormatInfo fmt1;
        Myfmt = MyCulture.DateTimeFormat;
        fmt1  = DateTimeFormatInfo.CurrentInfo;
        Console.WriteLine(Myfmt.GetMonthName(12));
        Console.WriteLine(fmt1.GetMonthName(12));
     }

}


 </source>


DateTimeFormatInfo.GetAllDateTimePatterns

<source lang="csharp"> using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Security.Cryptography; public class MainClass {

   public static void Main()
   {
       DateTime dt1 = new DateTime(2004, 10, 9, 22, 47, 35, 259);
       DateTimeFormatInfo di = new DateTimeFormatInfo();
       for (char c = "a"; c <= "z"; c++)
       {
           try
           {
               foreach (string s in di.GetAllDateTimePatterns(c))
               {
                   Console.WriteLine(""{0}": {1} - {2}/{3}", c, s,
                       dt1.ToString(c.ToString()), dt1.ToString(s));
               }
               char cUpper = Char.ToUpper(c);
               foreach (string s in di.GetAllDateTimePatterns(cUpper))
               {
                   Console.WriteLine(""{0}": {1} - {2}", cUpper, s,
                       dt1.ToString(cUpper.ToString()), dt1.ToString(s));
               }
           }
           catch (ArgumentException)
           {
               // Ignore--specifier not found.
           }
       }
   }

}



 </source>


DateTimeFormatInfo.LongDatePattern

<source lang="csharp">

using System; using System.Globalization; using System.IO; class MainClass {

 public static void Main() 
 {
   CultureInfo ci = new CultureInfo("nl-BE");
   FileStream outStream = File.Create("CultureInfo.txt");
   StreamWriter sw = new StreamWriter(outStream);
   sw.WriteLine("Native Name: " + ci.NativeName);
   sw.WriteLine("English Name: " + ci.EnglishName);
   DateTimeFormatInfo dtfi = ci.DateTimeFormat;
   sw.WriteLine("Long date pattern: " + dtfi.LongDatePattern);
   NumberFormatInfo nfi = ci.NumberFormat;
   sw.WriteLine("Currency symbol: " + nfi.CurrencySymbol);
   sw.WriteLine("Decimal seperator: " + nfi.NumberDecimalSeparator);
   sw.Flush();
   sw.Close();
 }

}


 </source>


DateTimeFormatInfo.TimeSeparator

<source lang="csharp">

using System; using System.Globalization; using System.Threading; class Class1 {

 static void Main(string[] args) {
     CultureInfo MyCulture = new CultureInfo("fr-FR");
     DateTime MyDate = System.DateTime.Now;
     Thread.CurrentThread.CurrentCulture = MyCulture;
     DateTimeFormatInfo dtf = MyCulture.DateTimeFormat;
     //Change date and time separator
     dtf.DateSeparator = "\\";
     dtf.TimeSeparator = "&";
     Console.WriteLine();
     Console.WriteLine(MyDate.ToLongDateString());
     Console.WriteLine(MyDate.ToLongTimeString());
     Console.WriteLine(MyDate.ToShortDateString());
     Console.WriteLine(MyDate.ToShortTimeString());
  }

}


 </source>