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.

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

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.

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

 LblDate.Text = ReturnDate()

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

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 %>

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!

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);

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;
                }
            }
commented: very nice! +12

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);
    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.