简体   繁体   中英

C# Date Time Formatting

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

  1. Mmm dd yyyy
  2. dd Month yyyy

I am currently doing object.GetDateTimeFormats('D')[1].ToString()

This is giving me January 31, 2012. But I should be able to get these two things:

  1. Jan 31, 2012
  2. 31 January, 2012

Use a custom DateTime formatting string:

// 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
  • MMMM - gives you January, February, March
Console.WriteLine(DateTime.Now.ToString("d-MMM-yy"));

18-Jan-18

Console.WriteLine(DateTime.Now.ToString("d-MM-yy"));

18-1-18

Console.WriteLine(DateTime.Now.ToString("d-MM-yyyy"));

18-1-2018

MoreDetails :- http://www.code-sample.net/CSharp/Format-DateTime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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