标签云

微信群

扫码加入我们

WeChat QR Code

I have a request to show a grid similar to a calendar showing on columns the day and on row months.Something like thisMonth |Sun|Mon|Tue|Wed|Thu|Fri|Sat|Sun|Mon|Tue|Wed|Thu|Fri|Sat|Sun|Mon|Tue|Wed|...|Thu|January |1|2|3|4|5|6|7|8|9| 10| 11|...................| 31|Febrary | | | |1|2|3|4|5|6|7|...........................| 29|...December| 30| 31| | | | |1|2|3|4|............................Is there a way to do this, considering that The Start day of the year must be the real day name, not a constant and this must be generated dynamicallyI try this but my idea goes off!Private Sub GenerateCalendar(ByVal year As Integer)Dim colsCount As Integer = 3If Bisiesto(año) ThencolsCount = colsCount + 366ElsecolsCount = colsCount + 365End IfDim dtCalendar As New DataTabledtCalendar.Columns.Add("Mes")Dim dayMonthYear As Date = New Date(year , 1, 1)'Showing the distribution of the first monthWhile dayMonthYear.Year = año And dayMonthYear.Month = 1Dim dtColumn As DataColumn = New DataColumn()dtColumn.ColumnName = WeekdayName(Weekday(dayMonthYear)) & dayMonthYear.Day & dayMonthYear.MonthdtColumn.Caption = WeekdayName(Weekday(dayMonthYear))dtCalendar.Columns.Add(dtColumn)dayMonthYear = diaMesAño.AddDays(1)End WhileDim row As DataRow = dtCalendario.NewRow()'Here I need to distribute the days on its column daydtCalendario.Rows.Add(row)wdgCalendario.DataSource = dtCalendarwdgCalendario.DataBind()End SubPublic Function LeapYear(ByVal year As Integer)Dim isLeapYear As Boolean = FalseIf year Mod 4 = 0 ThenIf (year Mod 100 = 0) And Not (year Mod 400 = 0) ThenisLeapYear = FalseElseisLeapYear = TrueEnd IfElseisLeapYear = FalseEnd IfReturn isLeapYear End Function


Suggestion: you can use DateTime.IsLeapYear(year) to determine if it is a leap year or not.

2019年04月18日06分04秒

This is great! thanks!

2019年04月19日06分04秒

How do you add a first column to put the name of the month?

2019年04月18日06分04秒

Juan: Updated my answer. Maybe there is a simpler way to put the monthname to the first column but it should work.

2019年04月19日06分04秒

I got an error when change the year

2019年04月19日06分04秒

Juan: Wow, your calendar is more challenging than i thought ;) Updated the complete method. It was caused by the calculation of the difference between two weekdays, so when first weekday of year is a friday and a month's first weekday is a monday i cannot subract both because the latter must be in the next week.

2019年04月19日06分04秒