简体   繁体   English

C#日期时间格式

[英]C# Date Time Formatting

How can I convert my DateTime object to this kind of date format: 如何将我的DateTime对象转换为这种日期格式:

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

  1. Jan 31, 2012 2012年1月31日
  2. 31 January, 2012 2012年1月31日

Use a custom DateTime formatting 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. 此处列出了所有自定义日期/时间格式

All types of date formatting you need. 您需要的所有类型的日期格式。

Just select the correct string format you need: 只需选择所需的正确字符串格式:

  • MMM - gives you Jan, Feb, Mar MMM - 给你一月,二月,三月
  • MMMM - gives you January, February, March MMMM - 给你1月,2月,3月
Console.WriteLine(DateTime.Now.ToString("d-MMM-yy"));

18-Jan-18 18-JAN-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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM