Solved

DateTime Format String

Posted on 2006-07-21
9
1,855 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
Windows Service with UDP 2 28
Image(2) 3 29
getting id from database 5 24
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

13 Experts available now in Live!

Get 1:1 Help Now