Solved

DateTime Format String

Posted on 2006-07-21
9
1,857 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now