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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:47:50  来源:igfitidea点击:

C# Date Time Formatting

c#datetime

提问by Varun Sharma

How can I convert my DateTimeobject to this kind of date format:

如何将我的DateTime对象转换为这种日期格式:

  1. Mmm dd yyyy
  2. dd Month yyyy
  1. 嗯 dd yyyy
  2. 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 日。但我应该能够得到这两件事:

  1. Jan 31, 2012
  2. 31 January, 2012
  1. 2012 年 1 月 31 日
  2. 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

更多细节:- 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);