Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i format 07/03/2012 to March 7th,2012 in c# [duplicate]

Any one please help i need to show the date 03/03/2012 as March 3rd,2012 etc

like image 823
Sreenath Plakkat Avatar asked Mar 07 '12 12:03

Sreenath Plakkat


People also ask

How do I format system date?

YYYY-MM-DD (2014-12-01)

How do you convert DateTime to mm dd yyyy?

For the data load to convert the date to 'yyyymmdd' format, I will use CONVERT(CHAR(8), TheDate, 112). Format 112 is the ISO standard for yyyymmdd.


1 Answers

You can create your own custom format provider to do this:

public class MyCustomDateProvider: IFormatProvider, ICustomFormatter {     public object GetFormat(Type formatType)     {         if (formatType == typeof(ICustomFormatter))             return this;          return null;     }      public string Format(string format, object arg, IFormatProvider formatProvider)     {         if (!(arg is DateTime)) throw new NotSupportedException();          var dt = (DateTime) arg;          string suffix;          if (new[] {11, 12, 13}.Contains(dt.Day))         {             suffix = "th";         }         else if (dt.Day % 10 == 1)         {             suffix = "st";         }         else if (dt.Day % 10 == 2)         {             suffix = "nd";         }         else if (dt.Day % 10 == 3)         {             suffix = "rd";         }         else         {             suffix = "th";         }          return string.Format("{0:MMMM} {1}{2}, {0:yyyy}", arg, dt.Day, suffix);     } } 

This can then be called like this:

var formattedDate = string.Format(new MyCustomDateProvider(), "{0}", date); 

Resulting in (for example):

March 3rd, 2012

like image 162
Rob Levine Avatar answered Sep 19 '22 02:09

Rob Levine