Sevyt 0 Newbie Poster

Probably a simple answer to a stupid question.
But i cannot figure it out myself.

I am using

Date.Now().ToString("D")

on a label.
Now i was wondering how you could use a similar string.
But to add 1,2,3 ect... to that.
So that i can have a label for each day of the week in the same format.

Dani AI

Generated

— You can increment the displayed date with DateTime.AddDays and write the result into each label. AddDays returns a new DateTime, so use that value to fill Label.Text. For a single label example:

' show tomorrow in a label
LabelTomorrow.Text = DateTime.Now.AddDays(1).ToLongDateString()

If you have one label per day, put the Label controls into an array and loop. Do this inside Page_Load only when the page is first loaded so postbacks don't keep resetting the labels:

If Not IsPostBack Then
    Dim labels As Label() = {Label1, Label2, Label3, Label4, Label5, Label6, Label7}
    For i As Integer = 0 To labels.Length - 1
        labels(i).Text = DateTime.Now.AddDays(i).ToLongDateString()
    Next
End If

Notes and cautions: AddDays accepts positive and negative values and fractional days. ToLongDateString uses the server's culture and time zone; to control formatting or culture explicitly use the ToString overload with a CultureInfo, and if you need the visitor's local time render the date client-side (JavaScript) or send the client timezone to the server. See the .NET docs for DateTime.AddDays, DateTime.ToLongDateString and the ASP.NET page lifecycle guidance on using IsPostBack (initialization) for more context.

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.