C# 将日期时间转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17526239/
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
Converting DateTime to string
提问by Nomesh Gajare
I want to convert DateTimeto String.
我想转换DateTime为String.
check the below code.
检查下面的代码。
namespace TestDateConvertion
{
class Program
{
static void Main(string[] args)
{
object value = new DateTime(2003,12,23,6,22,30);
DateTime result = (DateTime)value;
Console.WriteLine(result.ToString("dd/MM/yyyy"));
Console.ReadLine();
}
}
}
I have changed my system date format to Faeroese.
我已将系统日期格式更改为 Faeroese。
and I am getting the output as
我得到的输出为
23-12-2013
23-12-2013
How should I get the output as ?
我应该如何获得输出?
23/12/2013
23/12/2013
And consider this another scenario, suppose, i have a Customculture Info , and i want to convert my date w.r.t my custom culture, what i was doing before was as follows,
考虑另一种情况,假设我有一个 Customculture Info ,并且我想将我的日期转换为我的自定义文化,我之前所做的如下,
string.Format(customCulture, "{0:G}", result);
now how shall i get the datetime in string using customCulture and it should not depend on system DateTime?
现在我应该如何使用 customCulture 获取字符串中的日期时间并且它不应该依赖于系统日期时间?
采纳答案by Soner G?nül
Looks like your culture's date separator is -and as Tim pointed, /replaces itself with it.
看起来你的文化的日期分隔符是-,正如蒂姆指出的那样,/用它替换自己。
You should use CultureInfo.InvariantCultureas a second parameter in your result.ToString()method.
您应该CultureInfo.InvariantCulture在result.ToString()方法中用作第二个参数。
Gets the CultureInfo object that is culture-independent(invariant).
获取与区域性无关(不变)的 CultureInfo 对象。
object value = new DateTime(2003, 12, 23, 6, 22, 30);
DateTime result = (DateTime)value;
Console.WriteLine(result.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
Output will be;
输出将是;
23/12/2003
Here a DEMO.
这里有一个演示。
回答by Kamil Budziewski
Try this one
试试这个
Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
回答by JDunkerley
You could use the invariant culture:
您可以使用不变文化:
Console.WriteLine(
result.ToString("dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture
);
回答by wingyip
try
尝试
string.Format("{0:dd/MM/yyyy}",result)
Good luck
祝你好运
Wing
翅膀
回答by Usman
You need to add this
你需要添加这个
Console.WriteLine(result.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
Now your Code becomes
现在你的代码变成
object value = new DateTime(2003, 12, 23, 6, 22, 30);
DateTime result = (DateTime)value;
Console.WriteLine(result.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
Console.ReadLine();
NOTE*Add using System.Globalization;*
注意*使用 System.Globalization 添加;*
回答by Oleksii Aza
I fully agree with Tim Schmelter's comment and Soner G?nül's answer. Just wanted to add that when you are using Date Time format you should specify culture because by default culture will be get from Thread.CurrentThread.CurrentCulture(culture set in Control Panel->Region and Languages->Format), that means that with different settings your input will be different.
我完全同意 Tim Schmelter 的评论和 Soner G?nül 的回答。只是想补充一点,当您使用日期时间格式时,您应该指定文化,因为默认情况下文化将从Thread.CurrentThread.CurrentCulture(文化设置在Control Panel->Region and Languages->Format)中获取,这意味着使用不同的设置您的输入会有所不同。
Take a look on your example with different cultures:
看看你在不同文化中的例子:
object value = new DateTime(2003, 12, 23, 6, 22, 30);
DateTime result = (DateTime)value;
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
Console.WriteLine(result.ToString("dd/MM/yyyy", culture));
}
回答by stefano m
'/' is a special char that means "regional settings date separator". if you want use it like a normal char you can quote it using the quoter chart '\' Example:
'/' 是一个特殊字符,表示“区域设置日期分隔符”。如果你想像普通字符一样使用它,你可以使用引用图表 '\' 引用它,例如:
DateTime.Now.ToString(@"dd\/MM\/yyyy")

