Stem Cells Blood Banks Insurances .Net Hosting Credit Cards PC Rental Business
Tutorials: ASP .NET C# .NET VB .NET Articles: ASP .NET C# .NET VB .NET Fast Code: ASP .NET C# .NET VB .NET
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 = "nd";
else if (date.Day.ToString().EndsWith("3"))
ending = "rd";
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
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
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
Thanks for feedback!
Code is updated.
01/05/2007 15:26:47 UTC
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
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