如何在 C# 中将 DateTime 对象转换为 dd/mm/yyyy?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/941502/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:51:33  来源:igfitidea点击:

How to convert DateTime object to dd/mm/yyyy in C#?

c#

提问by

Possible Duplicates:
Convert a string to a date in .net
format date in c#
What markup is used to format StackOverflow questions?

可能的重复项:在 C#
中将字符串转换为 .net
格式日期的日期
什么标记用于格式化 StackOverflow 问题?

How to convert DateTime object to dd/mm/yyyy in C#?

如何在 C# 中将 DateTime 对象转换为 dd/mm/yyyy?

回答by Welbog

Are you talking about converting to a string for printing or something?

您是在谈论转换为用于打印的字符串还是什么?

String s = DateTime.ToString("dd/MM/yyyy");

And to be complete, here is more information about DateTime.ToStringand DateTime formatting in general.

为了完整起见,这里有更多关于DateTime.ToStringDateTime 格式的一般信息

回答by BFree

DateTime d = DateTime.Now;
string s = d.ToString("dd/MM/yyyy");
Console.WriteLine(s);

回答by Jon Skeet

One thing to note in addition to the other answers - / is a format character itself, representing the local date separator. If you want to make absolutelysure it uses an actual slash, either use the invariant culture (which uses a slash):

除了其他答案之外还需要注意的一件事 - / 是格式字符本身,代表本地日期分隔符。如果您想绝对确保它使用实际斜杠,请使用不变区域性(使用斜杠):

string s = dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

or escape the slashes:

或逃避斜线:

string s = dateTime.ToString("dd'/'MM'/'yyyy");