在 C# 中格式化日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/501460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
format date in c#
提问by Gold
How can I format a date as dd/mm/yyyyor mm/dd/yy?
如何将日期格式化为dd/mm/yyyy或mm/dd/yy?
Like in VB format("dd/mm/yy",now)
就像在 VB 中一样 format("dd/mm/yy",now)
How can I do this in C#?
我怎样才能在 C# 中做到这一点?
回答by M4N
It's almost the same, simply use the DateTime.ToString()method, e.g:
几乎一样,只需使用该DateTime.ToString()方法,例如:
DateTime.Now.ToString("dd/MM/yy");
Or:
或者:
DateTime dt = GetDate(); // GetDate() returns some date
dt.ToString("dd/MM/yy");
In addition, you might want to consider using one of the predefined date/time formats, e.g:
此外,您可能需要考虑使用预定义的日期/时间格式之一,例如:
DateTime.Now.ToString("g");
// returns "02/01/2009 9:07 PM" for en-US
// or "01.02.2009 21:07" for de-CH
These ensure that the format will be correct, independent of the current locale settings.
这些确保格式正确,独立于当前的区域设置。
Check the following MSDN pages for more information
查看以下 MSDN 页面以获取更多信息
Some additional, related information:
一些额外的相关信息:
If you want to display a date in a specific locale / culture, then there is an overload of the ToString()method that takes an IFormatProvider:
如果要在特定语言环境/文化中显示日期,则该ToString()方法有一个重载IFormatProvider:
DateTime dt = GetDate();
dt.ToString("g", new CultureInfo("en-US")); // returns "5/26/2009 10:39 PM"
dt.ToString("g", new CultureInfo("de-CH")); // returns "26.05.2009 22:39"
Or alternatively, you can set the CultureInfoof the current thread prior to formatting a date:
或者,您可以CultureInfo在格式化日期之前设置当前线程的 :
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
dt.ToString("g"); // returns "5/26/2009 10:39 PM"
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-CH");
dt.ToString("g"); // returns "26.05.2009 22:39"
回答by Arjan Einbu
string.Format("{0:dd/MM/yyyy}", DateTime.Now)
Look up "format strings" on MSDN to see all formatting options.
在 MSDN 上查找“格式字符串”以查看所有格式选项。
Use yy, yyyy, M, MM, MMM, MMMM, d, dd, ddd, ddddfor the date component
使用yy, yyyy, M, MM, MMM, MMMM, d, dd, ddd,dddd作为日期组件
Use h, hh, H, HH, m, mm, s, ssfor the time-of-day component
使用h, hh, H, HH, m, mm, s,ss作为时间组件
回答by Canavar
Try this :
尝试这个 :
String.Format("{0:MM/dd/yyyy}", DateTime.Now); // 01/31/2009
String.Format("{0:dd/MM/yyyy}", DateTime.Now); // 31/01/2009
String.Format("{dd/MM/yyyy}", DateTime.Now); // 31/01/2009
回答by Sorskoot
In you can also write
在你也可以写
DateTime aDate = new DateTime();
string s = aDate.ToShortDateString();
for a short notation
简短的表示法
or
或者
DateTime aDate = new DateTime();
string s = aDate.ToLongDateString();
for a long notation like "Sunday, Febuary 1, 2009".
对于像“2009 年 2 月 1 日星期日”这样的长符号。
Or take a look at MSDNfor the possibities of .ToString("???");
或者查看MSDN以了解 .ToString("???"); 的可能性。
回答by Nir
Better yet, use just
更好的是,只使用
DateTime.Now.ToString()
or
或者
DateTime.Now.ToString(CultureInfo.CurrentCulture)
to use the format the user prefers.
使用用户喜欢的格式。
回答by user2146538
I ran into the same issue. What I needed to do was add a reference at the top of the class and change the CultureInfo of the thread that is currently executing.
我遇到了同样的问题。我需要做的是在类的顶部添加一个引用并更改当前正在执行的线程的 CultureInfo。
using System.Threading;
string cultureName = "fr-CA";
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
DateTime theDate = new DateTime(2015, 11, 06);
theDate.ToString("g");
Console.WriteLine(theDate);
All you have to do is change the culture name, for example: "en-US" = United States "fr-FR" = French-speaking France "fr-CA" = French-speaking Canada etc...
您所要做的就是更改文化名称,例如:“en-US”=美国“fr-FR”=法语法国“fr-CA”=法语加拿大等...
回答by Arun Prasad E S
I think this is simple as you can convert to and from any format without any confusion
我认为这很简单,因为您可以在任何格式之间进行转换,而不会造成任何混淆
DateTime.ParseExact(txt.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyy/MM/dd"));

