How to create a Date Format with suffex like 1st, 2nd, 3rd, 4th

Written by admin on Mar 24, 2007
Create a Date having format with special suffex like st, nd, rd, th

Explanation:

Some time we want to display our date in specail string Format.
e.g

1st March 2007, 2nd of the July 2006 ...

suppose, GetDateofBirth (Username) is a method which returns the birthday of a user. Where userName is our user name which was taken either from MembershipUser object or any other source.

                      DateTime date = GetDateofBirth(name);

       string ending = string.Empty;

 

       if (date.Day.ToString().EndsWith("1"))

            {

                if (date.Day.ToString().StartsWith("1") && date.Day != 1)

                    ending = "th";

                else

                    ending = "st";

            }

            else if (date.Day.ToString().EndsWith("2"))

            {

                if (date.Day.ToString().StartsWith("1"))

                    ending = "th";

                else

                    ending = "nd";

            }

            else if (date.Day.ToString().EndsWith("3"))

            {

                if (date.Day.ToString().StartsWith("1"))

                    ending = "th";

                else

                    ending = "rd";

            }

            else

                ending = "th";

 

Now, we have a label control lbldatetime to which we are going to assign the date.

lbldatetime.Text= date.DayOfWeek.ToString() +@", " + date.ToString("dd").TrimStart(new char[]{'0'})+ending+@" " +date.ToString("MMMM");


This will display the Date Time like;

Sunday, 5th March 2007

Visitors/Readers Comments
(for questions please use The Forum)



JoshStodola

I am sorry, but that will not be very effective.  Consider the numbers 11-13.

They will result in 11st, 12nd, and 13rd.

That is no good!!!

30/04/2007 07:55:00 UTC

Hameed

Good Point!

For this we need some more if statements inside the first one.  i.e also use startwith( ) method for 11-13. 

30/04/2007 11:29:16 UTC

admin

Thanks for feedback!

Code is updated.

01/05/2007 15:26:47 UTC

Josh Stodola

Clean it up... 

Dim strDay as String = date.Day

If strDay.EndsWith("1") And strDay <> "11" Then
    lblDay.Text += "st"
ElseIf strDay.EndsWith("2") And strDay <> "12" Then
    lblDay.Text += "nd"
ElseIf strDay.EndsWith("3") And strDay <> "13" Then
    lblDay.Text += "rd"
Else
    lblDay.Text += "th"
End If

Regards,

22/05/2007 09:09:04 UTC

DestruKtor
List form formates codes:

 d :08/17/2000
' D :Thursday, August 17, 2000
' f :Thursday, August 17, 2000 16:32
' F :Thursday, August 17, 2000 16:32:32
' g :08/17/2000 16:32
' G :08/17/2000 16:32:32
' m :August 17
' r :Thu, 17 Aug 2000 23:32:32 GMT
' s :2000-08-17T16:32:32
' t :16:32
' T :16:32:32
' u :2000-08-17 23:32:32Z
' U :Thursday, August 17, 2000 23:32:32
' y :August, 2000
' dddd, MMMM dd yyyy :Thursday, August 17 2000
' ddd, MMM d "'"yy :Thu, Aug 17 '00
' dddd, MMMM dd :Thursday, August 17
' M/yy :8/00
' dd-MM-yy :17-08-00

05/10/2007 10:15:43 UTC




Add your Comments

Name:  
Message:


Advertise Here
For more details
Contact Us

Advertisments