Csharp/C Sharp/Development Class/Date Time Format
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
- 1 Compare the results of choosing InvariantInfo with those of choosing CurrentInfo:
- 2 DateTime ToString with CultureInfo
- 3 demonstrates culture formatting
- 4 Format time and date information 1
- 5 Format time and date information 2
- 6 Request a specific culture (english language in Great Britain):
- 7 The next example formats a DateTime with invariant culture. Invariant culture is always the same, regardless of the computer"s settings:
- 8 use the FromFileTime() method to convert an operating system file timestamp to a DateTime
- 9 use the ToFileTime() method to convert a DateTime to an operating system file timestamp
- 10 use the ToLongTimeString() and ToShortTimeString() methods to convert the time parts of a DateTime to long and short time strings
- 11 use the ToString() method to convert a DateTime to a string
Compare the results of choosing InvariantInfo with those of choosing CurrentInfo:
using System;
using System.Globalization;
public class MainClass {
public static void Main() {
DateTime dt = new DateTime();
DateTimeFormatInfo dtfi;
Console.Write("[I]nvariant or [C]urrent Info?: ");
if (Console.Read() == "I")
dtfi = DateTimeFormatInfo.InvariantInfo;
else
dtfi = DateTimeFormatInfo.CurrentInfo;
dtfi = DateTimeFormatInfo.InvariantInfo;
Console.WriteLine(dt.ToString("D", dtfi));
Console.WriteLine(dt.ToString("f", dtfi));
Console.WriteLine(dt.ToString("F", dtfi));
Console.WriteLine(dt.ToString("g", dtfi));
Console.WriteLine(dt.ToString("G", dtfi));
Console.WriteLine(dt.ToString("m", dtfi));
Console.WriteLine(dt.ToString("r", dtfi));
Console.WriteLine(dt.ToString("s", dtfi));
Console.WriteLine(dt.ToString("t", dtfi));
Console.WriteLine(dt.ToString("T", dtfi));
Console.WriteLine(dt.ToString("u", dtfi));
Console.WriteLine(dt.ToString("U", dtfi));
Console.WriteLine(dt.ToString("d", dtfi));
Console.WriteLine(dt.ToString("y", dtfi));
Console.WriteLine(dt.ToString("dd-MMM-yy", dtfi));
}
}
DateTime ToString with CultureInfo
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"));
}
}
demonstrates culture formatting
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example21_10.cs demonstrates culture formatting
*/
using System;
using System.Globalization;
public class Example21_10
{
public static void Main()
{
// create a date and a currency value
DateTime dtNow = DateTime.Now;
Double curOriginal = 12345.67;
// and format the variables for a specific culture
CultureInfo ci = new CultureInfo("en-US");
string sLocalizedDate = dtNow.ToString("d", ci);
string sLocalizedCur = curOriginal.ToString("c", ci);
// print them out
Console.WriteLine(sLocalizedDate);
Console.WriteLine(sLocalizedCur);
// and format them for a second culture
CultureInfo ci2 = new CultureInfo("en-GB");
string sLocalizedDate2 = dtNow.ToString("d", ci2);
string sLocalizedCur2 = curOriginal.ToString("c", ci2);
// print them out again
Console.WriteLine(sLocalizedDate2);
Console.WriteLine(sLocalizedCur2);
}
}
Format time and date information 1
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Format time and date information.
using System;
public class CustomTimeAndDateFormatsDemo {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("Time is {0:hh:mm tt}", dt);
Console.WriteLine("24 hour time is {0:hh:mm}", dt);
Console.WriteLine("Date is {0:ddd MMM dd, yyyy}", dt);
Console.WriteLine("Era: {0:gg}", dt);
Console.WriteLine("Time with seconds: " +
"{0:HH:mm:ss tt}", dt);
Console.WriteLine("Use m for day of month: {0:m}", dt);
Console.WriteLine("use m for minutes: {0:%m}", dt);
}
}
Format time and date information 2
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Format time and date information.
using System;
public class TimeAndDateFormatDemo {
public static void Main() {
DateTime dt = DateTime.Now; // obtain current time
Console.WriteLine("d format: {0:d}", dt);
Console.WriteLine("D format: {0:D}", dt);
Console.WriteLine("t format: {0:t}", dt);
Console.WriteLine("T format: {0:T}", dt);
Console.WriteLine("f format: {0:f}", dt);
Console.WriteLine("F format: {0:F}", dt);
Console.WriteLine("g format: {0:g}", dt);
Console.WriteLine("G format: {0:G}", dt);
Console.WriteLine("m format: {0:m}", dt);
Console.WriteLine("M format: {0:M}", dt);
Console.WriteLine("r format: {0:r}", dt);
Console.WriteLine("R format: {0:R}", dt);
Console.WriteLine("s format: {0:s}", dt);
Console.WriteLine("u format: {0:u}", dt);
Console.WriteLine("U format: {0:U}", dt);
Console.WriteLine("y format: {0:y}", dt);
Console.WriteLine("Y format: {0:Y}", dt);
}
}
Request a specific culture (english language in Great Britain):
using System;
using System.Globalization;
public class MainClass {
public static void Main() {
CultureInfo uk = CultureInfo.GetCultureInfo("en-GB");
Console.WriteLine(3.ToString("C", uk)); // ?¡ê3.00
}
}
The next example formats a DateTime with invariant culture. Invariant culture is always the same, regardless of the computer"s settings:
using System;
using System.Globalization;
public class MainClass {
public static void Main() {
DateTime dt = new DateTime(2000, 1, 2);
CultureInfo iv = CultureInfo.InvariantCulture;
Console.WriteLine(dt.ToString(iv)); // 01/02/2000 00:00:00
Console.WriteLine(dt.ToString("d", iv)); // 01/02/2000
}
}
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 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 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"));
}
}