0

Hello

I would be grateful for some assistance getting the date to appear on my page, please.

This is the code in my aspx.vb file:

Dim Label As Object

    Function ReturnDate() As String

        Dim theDate As String = ""

        Dim NumericDayofMonth As Integer = Date.Now.Day
        Dim OrdinalSuffix As String = ""

        Select Case NumericDayofMonth
            Case 1, 21, 31
                OrdinalSuffix = "<sup>st</sup>"
            Case 2, 22
                OrdinalSuffix = "<sup>nd</sup>"
            Case 3, 23
                OrdinalSuffix = "<sup>rd</sup>"
            Case Else
                OrdinalSuffix = "<sup>th</sup>"
        End Select

        Dim NumericMonthofYear As Integer = Date.Now.Month
        Dim MonthofYear As String = ""

        Select Case NumericMonthofYear
            Case 1
                MonthofYear = "January"
            Case 2
                MonthofYear = "February"
            Case 3
                MonthofYear = "March"
            Case 4
                MonthofYear = "April"
            Case 5
                MonthofYear = "May"
            Case 6
                MonthofYear = "June"
            Case 7
                MonthofYear = "July"
            Case 8
                MonthofYear = "August"
            Case 9
                MonthofYear = "September"
            Case 10
                MonthofYear = "October"
            Case 11
                MonthofYear = "November"
            Case 12
                MonthofYear = "December"
        End Select

        theDate &= DateTime.Now.Day.ToString() '1, 2, 3, 4 etc
        theDate &= OrdinalSuffix & " " 'st, nd, rd, th
        theDate &= MonthofYear & " " 'Jan, Feb etc
        theDate &= DateTime.Now.Year.ToString() '2013, 2014 etc

        Return theDate
    End Function

I also have this line to show the date, but I am not sure where to place it in the aspx.vb file:

  LblDate.Text = ReturnDate()

And in my aspx file, I have:

</style>

     <asp:Label ID="LblDate" runat="server" class="labelStyle" Text="Label"></asp:Label>

    <h1 class="dima">Text</h1>  

Thanks for any help.

Edited by JorgeM: moved to the asp.net forum category

3
Contributors
9
Replies
34
Views
3 Years
Discussion Span
Last Post by JorgeM
0

Ok, so if you are willing to give up the ordinal suffix, all of the code above can be replaced with one line of code.

Simply place this link of code in your Page Load event block (in aspx.vb file)

LblDate.Text = DateTime.Now.ToString("MMMM dd, yyyy")

If you are interested in reading more about how to format Date and Time, see...
Custom Date and Time Format Strings

Edited by JorgeM

0

Hello Jorge

Thanks again.

No, I can't give up the ordinals. It's how we write the date.

Is there such as thing as Return. Date Page load?

Thank you again.

0

Ok then assuming your "ReturnDate" method works, just place the filling line in your Page Load sub-routine.

 LblDate.Text = ReturnDate()
0

That seems to work fine now, Jorge, thanks!

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LblDate.Text = ReturnDate()

    End Sub
0

Are you looking for a certain format?

This is what I do to keep the copyright set to the current year ...

<%= DateTime.Now.Year %>

If you need the whole date ...

<%= DateTime.Now.ToShortDateString %>

0

Thanks, ggamble

I will bear that in mind. I was looking for a way of displaying the ordinal date and think it's (sort of) working now.

Thanks again!

0

Ah ... understand now. Your best bet would be to make a custom formatter. Here is one I've used. Stole the idea from someone awhile ago ... :-)

    public class FormatOrdinal : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type fType)
        {
            if (fType == typeof(ICustomFormatter))
                return this;

            return null;
        }

        public string Format(string format, object obj, IFormatProvider formatProvider)
        {
            if (!(obj is DateTime)) throw new Exception();

            var dat = (DateTime)obj;

            string ord;

            switch (dat.Day % 10)
            {
                case 1:
                    ord = "st";
                    break;
                case 2:
                    ord = "nd";
                    break;
                case 3:
                    ord = "rd";
                    break;
                default:
                    ord = "th";
                    break;
            }

            return string.Format("{0:MMMM} {1}{2}, {0:yyyy}", obj, dat.Day, ord);
        }
    }
}

Use it like this ...

string.Format(new FormatOrdinal(), "{0}", SomeDate);

1

oops, modded version :-)

    if (dat.Day == 11 || dat.Day == 12 || dat.Day == 13)
            {
                ord = "th"; 
            }
            else
            {
                switch (i)
                {
                    case 1:
                        ord = "st";
                        break;
                    case 2:
                        ord = "nd";
                        break;
                    case 3:
                        ord = "rd";
                        break;
                    default:
                        ord = "th";
                        break;
                }
            }
Votes + Comments
very nice!
1

Nice job ggamble... i posted your code here as a whole so its easier for someone else that may come along and have interest in this. Also fixed line 7 from your last sample code (it was correct on line 19 from your previous post).

public class FormatOrdinal : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter))
            return this;
        else
            return null;
    }


    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        if (!(arg is DateTime)) throw new Exception();
        var dat = (DateTime)arg;
        string ord;
        if (dat.Day == 11 || dat.Day == 12 || dat.Day == 13)
        {
            ord = "th";
        }
        else
        {
            switch (dat.Day % 10)
            {
                case 1:
                    ord = "st";
                    break;
                case 2:
                    ord = "nd";
                    break;
                case 3:
                    ord = "rd";
                    break;
                default:
                    ord = "th";
                    break;
            }
        }
            return string.Format("{0:MMMM} {1}{2}, {0:yyyy}", arg, dat.Day, ord);
    }

}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.