How to Format 07/03/2012 to March 7Th,2012 in C#

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

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

Getting day suffix when using DateTime.ToString()

As a reference I always use/refer to [SteveX String Formatting] 1
and there doesn't appear to be any "th" in any of the available variables but you could easily build a string with

string.Format("{0:dddd dd}{1} {0:MMMM yyyy}", DateTime.Now, (?));

You would then have to supply a "st" for 1, "nd" for 2, "rd" for 3, and "th" for all others and could be in-lined with a "? :" statement.

var now = DateTime.Now;
(now.Day % 10 == 1 && now.Day % 100 != 11) ? "st"
: (now.Day % 10 == 2 && now.Day % 100 != 12) ? "nd"
: (now.Day % 10 == 3 && now.Day % 100 != 13) ? "rd"
: "th"

Is there an easy way to create ordinals in C#?

This page gives you a complete listing of all custom numerical formatting rules:

Custom numeric format strings

As you can see, there is nothing in there about ordinals, so it can't be done using String.Format. However its not really that hard to write a function to do it.

public static string AddOrdinal(int num)
{
if( num <= 0 ) return num.ToString();

switch(num % 100)
{
case 11:
case 12:
case 13:
return num + "th";
}

switch(num % 10)
{
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
}
}

Update: Technically Ordinals don't exist for <= 0, so I've updated the code above. Also removed the redundant ToString() methods.

Also note, this is not internationalized. I've no idea what ordinals look like in other languages.

C# convert Datetime to Thursday, 26th July 2014 format

I think you need to separate the parts before and behind th from each other:

DateTime dt = new DateTime(2014, 07, 26);
string result = string.Format("{1}{0} {2}",
dt.Day == 1 ? "st" : dt.Day == 2 ? "nd" : dt.Day == 3 ? "rd" : "th",
dt.ToString("dddd, dd", CultureInfo.InvariantCulture),
dt.ToString("MMMM yyyy", CultureInfo.InvariantCulture));

If you don't really need your exact format you can also use DateTime methods like ToLongDateString which uses the current culture and has a fixed format.

Re-format datetime to 7th November 2014

I don't believe there's any direct support for ordinals ("st", "nd", "th") within .NET. If you only need to support English, I suggest you hard code it yourself. For example:

string text = string.Format("{0}{1} {2} {3}", dt.Day, GetOrdinal(dt.Day),
dt.ToString("MMMM"), dt.Year);

(Where you'd write GetOrdinal yourself.) Note that this assumes you want exactly this format - different cultures (even within English) may prefer November 7th 2014 for example.

If you need to support all kinds of languages, it becomes very difficult - different languages have some very different approaches to ordinals.

Side-note: Even Noda Time doesn't handle this yet. I hope to eventually implement some CLDR support, which should in theory handle it for all locales. We'll see...

How to fetch just the date from DateTime Entity while iterating an Entity Framework Object

The problem is you are outputting the model value directly. If you want to use the display formatting, you should use the following helper instead.

@Html.DisplayFor(m => customers.BirthDate)

Here are some more details in this other question as to why.

When should I use Html.Displayfor in MVC



Related Topics



Leave a reply



Submit