Varun Sharma
Varun Sharma

Reputation: 2759

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

Upvotes: 7

Views: 40945

Answers (4)

Nayan_07
Nayan_07

Reputation: 195

To get the Format like - September 26, 2019

string.Format("{0:MMMM dd, yyyy}", YourDate);

Upvotes: 1

MAFAIZ
MAFAIZ

Reputation: 691

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

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180788

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.

Upvotes: 18

Bryan Hong
Bryan Hong

Reputation: 1483

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

Upvotes: 3

Related Questions