Pawan Lakhara
Pawan Lakhara

Reputation: 1150

display date from current dateto next six days

I am using this code for display dates from current date to next six days. if any other code for display date like this. please help

Private Sub Displaydate()
    cn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("DataConnectionString")
    lblDateday.Text = System.DateTime.Now.ToString("dddd")
    lblMonthdate.Text = System.DateTime.Now.ToString("dd MMMM ")
    lblDateday2.Text = System.DateTime.Now.AddDays(1).ToString("dddd")
    lblMonthdate2.Text = System.DateTime.Now.AddDays(1).ToString("dd MMMM ")
    lblDateday3.Text = System.DateTime.Now.AddDays(2).ToString("dddd")
    lblMonthdate3.Text = System.DateTime.Now.AddDays(2).ToString("dd MMMM ")
    lblDateday4.Text = System.DateTime.Now.AddDays(3).ToString("dddd")
    lblMonthdate4.Text = System.DateTime.Now.AddDays(3).ToString("dd MMMM ")
    lblDateday5.Text = System.DateTime.Now.AddDays(4).ToString("dddd")
    lblMonthdate5.Text = System.DateTime.Now.AddDays(4).ToString("dd MMMM ")
    lblDateday6.Text = System.DateTime.Now.AddDays(5).ToString("dddd")
    lblMonthdate6.Text = System.DateTime.Now.AddDays(5).ToString("dd MMMM ")
    lblDateday7.Text = System.DateTime.Now.AddDays(6).ToString("dddd")
    lblMonthdate7.Text = System.DateTime.Now.AddDays(6).ToString("dd MMMM ")
End Sub

the output is

Wednesday Thursday Friday Saturday Sunday Monday Tuesday

21 November 22 November 23 November 24 November 25 November 26 November 27November

Upvotes: 1

Views: 795

Answers (1)

WozzeC
WozzeC

Reputation: 2660

Put the things you handle into arrays and then do loops instead.

Assign Lists globally

Dim DateDayList as List(of Label) = new List(of Label)
Dim MonthDayList as List(of Label) = new List(of Label)

Add all dateDay labels to list in the correct order inside the Initialize sub.

DateDayList.Add(lblDateDay)
DateDayList.Add(lblDateDay2)
etc.

Do the same with the Month day labels.

Then simply do this:

for i as Integer = 0 To 6
   DateDayList(i).Text = System.DateTime.Now.AddDays(i).ToString("dddd")
   MonthDayList(i).Text = System.DateTime.Now.AddDays(i).ToString("dd MMMM ")
next

Upvotes: 1

Related Questions