C# 日期时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9074055/
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
C# Date Time Formatting
提问by Varun Sharma
How can I convert my DateTimeobject to this kind of date format:
如何将我的DateTime对象转换为这种日期格式:
- Mmm dd yyyy
- dd Month yyyy
- 嗯 dd yyyy
- dd 月 yyyy
I am currently doing object.GetDateTimeFormats('D')[1].ToString()
我目前正在做 object.GetDateTimeFormats('D')[1].ToString()
This is giving me January 31, 2012. But I should be able to get these two things:
这给了我 2012 年 1 月 31 日。但我应该能够得到这两件事:
- Jan 31, 2012
- 31 January, 2012
- 2012 年 1 月 31 日
- 2012 年 1 月 31 日
采纳答案by Robert Harvey
Use a custom DateTimeformatting string:
使用自定义DateTime格式字符串:
// Returns Jan 31, 2012
myDateTimeObject.ToString("MMM dd, yyyy");
// Returns 31 January, 2012
myDateTimeObject.ToString("dd MMMM, yyyy");
All of the custom date/time formats are listed here.
回答by Bryan Hong
All types of date formatting you need.
Just select the correct string format you need:
只需选择您需要的正确字符串格式:
- MMM - gives you Jan, Feb, Mar
- MMMM - gives you January, February, March
- MMM - 给你一月、二月、三月
- MMMM - 给你一月、二月、三月
回答by MAFAIZ
Console.WriteLine(DateTime.Now.ToString("d-MMM-yy"));
18-Jan-18
18 年 1 月 18 日
Console.WriteLine(DateTime.Now.ToString("d-MM-yy"));
18-1-18
18-1-18
Console.WriteLine(DateTime.Now.ToString("d-MM-yyyy"));
18-1-2018
18-1-2018
MoreDetails:- http://www.code-sample.net/CSharp/Format-DateTime
回答by Nayan_07
To get the Format like - September 26, 2019
要获得类似的格式 - 2019 年 9 月 26 日
string.Format("{0:MMMM dd, yyyy}", YourDate);

