Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Pls some one tell that how to display date in website the format is 27th july 2012
how to take date from database
Posted

Hi,
Try this:
C#
//String should be date and in dd MMM yyyy format
public static string GetOrdinal(string s)
{
    int Number = Convert.ToInt32(s.Split(' ')[0]);
    string suffix = String.Empty;
    if (Number.ToString().Length > 2)
    {
        int intEndNum = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length - 2, 2));
        if (intEndNum >= 11 && intEndNum <= 13)
            switch (intEndNum)
            {
                case 11:
                case 12:
                case 13:
                    suffix = "th";
                    break;
            }
    }


    if (Number >= 21)
    {
        //Handles 21st, 22nd, 23rd, et al
        int Number21 = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length - 1, 1));
        switch (Number21)
        {
            case 1:
                suffix = "st";
                break;
            case 2:
                suffix = "nd";
                break;
            case 3:
                suffix = "rd";
                break;
            case 0:
                suffix = "th";
                break;
            default:
                for (int i = 4; i <= 9; i++)
                {
                    if (Number21 == i)
                    {
                        suffix = "th";
                        break;
                    }
                    else
                        suffix = String.Empty;
                }
                break;
        }
    }
    else
    {
        switch (Number)
        {
            case 1:
                suffix = "st";
                break;
            case 2:
                suffix = "nd";
                break;
            case 3:
                suffix = "rd";
                break;
            default:
                for (int i = 4; i <= 21; i++)
                {
                    if (Number == i)
                    {
                        suffix = "th";
                        break;
                    }
                    else
                    {
                        suffix = String.Empty;
                    }
                }
                break;
        }
    }
    string NewDate = s.Split(' ')[0]+""+suffix + " " + s.Split(' ')[1] + " " + s.Split(' ')[2];
    return NewDate;
}

Call the above function from anywhere like:
C#
static void Main()
{
    string TodayDate = DateTime.Now.ToString("dd MMM yyyy");
    string date = GetOrdinal(TodayDate);
    Console.WriteLine(date);
}




--Amit
 
Share this answer
 
v2
Comments
Kenneth Haugland 8-Aug-12 1:35am    
And what about the third 3'rd ?
_Amy 8-Aug-12 1:49am    
Keenath, please try my updated answer.
1. ALWAYS ALWAYS store datetimes in SQL as UTC
2. See #1
3. Move the date down to the client and let the javascript[^] Date object format it for you according to locale and TZ
4. See #1

Storing UTC handles DST switches and makes everything consistent

My particular bugbear is having to read US dates - in my world, they're only right 12 times a year
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900