Solved

DateTime Format String

Posted on 2006-07-21
9
1,873 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 500 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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!
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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