Solved

DateTime Format String

Posted on 2006-07-21
9
1,868 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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