Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DateTime Format String

Posted on 2006-07-21
9
Medium Priority
?
1,888 Views
Last Modified: 2012-06-21
Hello everyone,

I need to figure out what the format string, in order to get the date displayed like this:

June 30th

Right now I have: date.ToString("MMMM d");

Does anyone know how to get the 'th' added onto the end of the day?
0
Comment
Question by:quanmac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 24

Expert Comment

by:Justin_W
ID: 17157829
There is no built-in support for the "st", "th", "rd", etc. suffixes for dates in .NET.

You would have to write your own custom function (or find one) to do this, and then insert the output of that function into the string.
0
 
LVL 8

Expert Comment

by:Maher-Jendasi
ID: 17157851
Format strings does not support "th" in dates.
the best thing is to write a function to format the date as "June 30" and append the "th" to it

public string CustomDateFormat(DateTime dateToFormat)
{
     return (dateToFormat.ToString("MMMM d") + "th");
}
0
 
LVL 8

Expert Comment

by:Maher-Jendasi
ID: 17157895
Or you can write the function as:

public string CustomDateFormat(DateTime dateToFormat)
{
     return (dateToFormat.ToString("MMMM d'th'");
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:gdupadhyay
ID: 17158990
Hi, There is no such functionality available with .net. You can write with ur own.
Means u can add "th" as suffix with date function.
Its batter u define a key in web.config file for that. u can used that key with all date. If u want to change that, u have to change at only one place and it reflect all place in ur project where date is used.

For Example
<add key="DateSuf" value="th">

Now used this as:

dtFromDate = dateToFormat.ToString("MMMM d") + DateSuf);
0
 
LVL 8

Expert Comment

by:Maher-Jendasi
ID: 17159186
quanmac,

Please note that if you go with gdupadhyay  suggestion (i.e adding the th as key in configuration file), you must modify the function as

string suffix = ConfigurationSettings.AppSettings["DateSuf"];
dtFromDate = dateToFormat.ToString("MMMM d") + suffix);
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 17159279
Hi,
use String.Format function
string.Format("{0:MMMM dd}",CurrentDay)+" th"
 -this will give u the exact result

      DateTime CurrentDay = new DateTime(2006, 6, 13);
      Response.Write(string.Format("{0:MMMM dd}",CurrentDay)+" th");


regards
pradeep
0
 
LVL 9

Expert Comment

by:jonorossi
ID: 17160730
Everyone except Justin missed the fact that not all days have 'th' after them. Try something like this:

    public string CustomFormatDate(DateTime date)
    {
        string daySuffix = "";
        if (date.Day == 1)
            daySuffix = "st";
        else if (date.Day == 2)
            daySuffix = "nd";
        else if (date.Day == 3)
            daySuffix = "rd";
        else
            daySuffix = "th";
       
        return (date.ToString("MMMM d") + daySuffix);
    }
0
 
LVL 9

Accepted Solution

by:
jonorossi earned 2000 total points
ID: 17162113
Sorry, I missed 21st, 22nd, 23rd. It would become this instead:

    public string CustomFormatDate(DateTime date)
    {
        string daySuffix = "";
        if (date.Day == 1 || date.Day == 21)
            daySuffix = "st";
        else if (date.Day == 2 || date.Day == 22)
            daySuffix = "nd";
        else if (date.Day == 3 || date.Day == 23)
            daySuffix = "rd";
        else
            daySuffix = "th";
       
        return (date.ToString("MMMM d") + daySuffix);
    }
0
 
LVL 3

Expert Comment

by:GilesBathgate
ID: 27606364
@jonorossi What about the 31st?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question