using System; using System.Globalization; using System.Threading; class Program { static void Main(string[] args) { DateTime d = new DateTime(2005, 08, 09); Console.WriteLine(d.ToLongDateString()); Console.WriteLine(d.ToString("D", new CultureInfo("fr-FR"))); CultureInfo ci = Thread.CurrentThread.CurrentCulture; Console.WriteLine(ci.ToString() + ": " + d.ToString("D")); ci = new CultureInfo("es-ES"); Thread.CurrentThread.CurrentCulture = ci; Console.WriteLine(ci.ToString() + ": " + d.ToString("D")); } }
Author: coder
use the ToFileTime() method to convert a DateTime to an operating system file timestamp
using System; class MainClass { public static void Main() { DateTime myDateTime10 = new DateTime(2004, 1, 15, 23, 2, 5); long myFileTime = myDateTime10.ToFileTime(); Console.WriteLine("myDateTime10.ToFileTime() = " + myDateTime10.ToFileTime()); } }
use the FromFileTime() method to convert an operating system file timestamp to a DateTime
using System; class MainClass { public static void Main() { DateTime myDateTime10 = new DateTime(2004, 1, 15, 23, 2, 5); long myFileTime = myDateTime10.ToFileTime(); DateTime myDateTime11 = DateTime.FromFileTime(myFileTime); Console.WriteLine("DateTime.FromFileTime() = " + myDateTime11); } }
use the ToLongTimeString() and ToShortTimeString() methods to convert the time parts of a DateTime to long and short time strings
using System; class MainClass { public static void Main() { DateTime myDateTime10 = new DateTime(2004, 1, 15, 23, 2, 5); long myFileTime = myDateTime10.ToFileTime(); DateTime myDateTime11 = DateTime.FromFileTime(myFileTime); Console.WriteLine("myDateTime11.ToLongTimeString() = " + myDateTime11.ToLongTimeString()); Console.WriteLine("myDateTime11.ToShortTimeString() = " + myDateTime11.ToShortTimeString()); } }
use the ToString() method to convert a DateTime to a string
using System; class MainClass { public static void Main() { DateTime myDateTime12 = new DateTime(2004, 1, 12, 22, 2, 10); Console.WriteLine("myDateTime12.ToString() = " + myDateTime12.ToString()); Console.WriteLine("myDateTime12.ToString("MMMM dd, yyyy") = " +myDateTime12.ToString("MMMM dd, yyyy")); Console.WriteLine("myDateTime12.ToString("d") = " + myDateTime12.ToString("d")); Console.WriteLine("myDateTime12.ToString("D") = " + myDateTime12.ToString("D")); Console.WriteLine("myDateTime12.ToString("f") = " + myDateTime12.ToString("f")); Console.WriteLine("myDateTime12.ToString("F") = " + myDateTime12.ToString("F")); Console.WriteLine("myDateTime12.ToString("g") = " + myDateTime12.ToString("g")); Console.WriteLine("myDateTime12.ToString("G") = " + myDateTime12.ToString("G")); Console.WriteLine("myDateTime12.ToString("m") = " + myDateTime12.ToString("m")); Console.WriteLine("myDateTime12.ToString("r") = " + myDateTime12.ToString("r")); Console.WriteLine("myDateTime12.ToString("s") = " + myDateTime12.ToString("s")); Console.WriteLine("myDateTime12.ToString("t") = " + myDateTime12.ToString("t")); Console.WriteLine("myDateTime12.ToString("T") = " + myDateTime12.ToString("T")); Console.WriteLine("myDateTime12.ToString("u") = " + myDateTime12.ToString("u")); Console.WriteLine("myDateTime12.ToString("U") = " + myDateTime12.ToString("U")); Console.WriteLine("myDateTime12.ToString("y") = " + myDateTime12.ToString("y")); } }
illustrates the CultureInfo class 2
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example21_7.cs illustrates the CultureInfo class */ using System; using System.Globalization; using System.IO; public class Example21_7 { public static void Main() { // create a CultureInfo object for the nl-BE culture CultureInfo ci = new CultureInfo("nl-BE"); // create a file to hold the results FileStream outStream = File.Create("CultureInfo.txt"); // use a StreamWriter to write data to the file StreamWriter sw = new StreamWriter(outStream); // show some basic information sw.WriteLine("Native Name: " + ci.NativeName); sw.WriteLine("English Name: " + ci.EnglishName); // get datetime formatting info DateTimeFormatInfo dtfi = ci.DateTimeFormat; sw.WriteLine("Long date pattern: " + dtfi.LongDatePattern); // get numeric formatting info NumberFormatInfo nfi = ci.NumberFormat; sw.WriteLine("Currency symbol: " + nfi.CurrencySymbol); sw.WriteLine("Decimal seperator: " + nfi.NumberDecimalSeparator); // flush and close the file sw.Flush(); sw.Close(); } }
new CultureInfo( “ru-RU” )
using System; using System.Globalization; public class EntryPoint { static void Main() { CultureInfo current = CultureInfo.CurrentCulture; CultureInfo germany = new CultureInfo( "de-DE" ); CultureInfo russian = new CultureInfo( "ru-RU" ); double money = 123.45; string localMoney = money.ToString( "C", current ); Console.WriteLine( localMoney+ " Local Money" ); localMoney = money.ToString( "C", germany ); Console.WriteLine( localMoney+ " German Money" ); localMoney = money.ToString( "C", russian ); Console.WriteLine( localMoney+ " Russian Money" ); } }