Generate 29 days of February in .NET - vb.net

I am trying to populate a dropdown with No.of days generated by the system by the following code
Sub LoadDay()
Try
ddlDay.Items.Clear()
For i As Integer = 1 To System.DateTime.DaysInMonth(DateTime.Today.Year, ddlMonth.SelectedValue)
ddlDay.Items.Add(New ListItem(i.ToString(), i.ToString("00")))
Next
ddlDay.Items.Insert(0, New ListItem("--Select--", -999))
Catch ex As Exception
End Try
End Sub
Problem is, for the month of February it shows only 28 days because the following code generates Days on the basis of current year and this year isn't a leap year
System.DateTime.DaysInMonth(DateTime.Today.Year, ddlMonth.SelectedValue)
How can I fix it to always show 29 days for the month of Februaray?

You can use DayInMonth in DateTime
Dim dayinMonth As String
dayinMonth = System.DateTime.DaysInMonth(2012, 2)
This allow you to know the day in month for 2015 February. Parameter inside stand for year and month

I'd be interested in knowing what your requirment is that you have to be able to select invalid days, but, if you don't want your days to change based on the year, just create your own array of days. This leaves your intent a bit more clear than putting an arbitrary year in the DaysInMonth function.
Dim Days = {31,28,31,30,31,30,31,31,30,31,30,31}
ddlDay.Items.Clear()
For i As Integer = 1 To Days(ddlMonth.SelectedValue-1)
ddlDay.Items.Add(New ListItem(i.ToString(), i.ToString("00")))
Next
ddlDay.Items.Insert(0, New ListItem("--Select--", -999))

Related

How to determine week number within calendar quarter period VB6

Having some trouble locating a function to determine the week number of a quarter period in a standard date based (i.e. Day 1 = 2017-01-01) calendar table.
My table has the following information stored :
Quarter Number, Beginning Date, End Date, Number of Days;
As an example, for the first quarter of calendar year, the result for Week 1 would be 1, Week 14 would be 1, going through each quarter until the final week of quarter 4.
Any ideas please help?
I don't believe there is a built-in function for this. If I understand what you're asking, this is a trivial function to write. You don't include any code so I'm just giving you the most basic example. You want to do something similar to this.
Public Function GetWeekInQuarter(ByVal WeekNumber As Integer) As Integer
Const intWeeksInQuarter = 13
Dim intResult As Integer
intResult = WeekNumber Mod intWeeksInQuarter
'if intResult <> intWeeksInQuarter = 0 then this is the 13th week
GetWeekInQuarter = IIf(intResult <> intWeeksInQuarter, intResult, intWeeksInQuarter)
End Function

Day360 in vb.net

I want to use the Days360 function in VB.Net. I need to know the difference in days between two dates assuming 360 days in a year (not 365 days the DateDiff function uses).
For example DateDiff(DateInterval.Day,"16/10/2015", "04/02/2016") = 111 days, but Days360 should return 109 days.
Days360 function in Excel calculates the days between two dates using a fictional calendar that has 30 days in each month. This method is used for some financial purposes.
You can write a function to do the same calculation.
[Edit]
Excel supports two versions of the calculation: one common in the US (this is the default) and the other common in Europe (see the documentation of the DAYS360 function for details).
The code I originally posted implemented the European version. I have updated it to support both versions. Thanks to Nikhil Vartak for pointing this out.
Function Days360(startDate As DateTime, endDate As DateTime, euMethod As Boolean) As Integer
Dim months As Integer = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month
If euMethod Then
'Use European method (start or end dates after the 30th of the month are changed to 30th)
Return months * 30 + Math.Min(30, endDate.Day) - Math.Min(30, startDate.Day)
Else 'Use US method
'If the start date is the last day of the month, change it to the 30th
Dim startDay As Integer = startDate.Day
startDay = If(startDate.Day >= DateTime.DaysInMonth(startDate.Year, startDate.Month), 30, startDate.Day)
'If end date is last of the month, change it to the 30th
Dim endDay As Integer = endDate.Day
endDay = If(endDate.Day >= DateTime.DaysInMonth(endDate.Year, endDate.Month), 30, endDate.Day)
'If end date is last of the month and start date is before 30th, change end date to 1st of the next month
If endDate.Day >= DateTime.DaysInMonth(endDate.Year, endDate.Month) And startDay < 30 Then
endDay = 1
months += 1
End If
Return months * 30 + endDay - startday
End If
End Function

Week number input returns crazy full week output

I would like some feedback on what's wrong with these codes. I'm trying to output a full week based on a week number. For instance if I input "2014/45" I would like to output all dates spanning from November 2nd to November 8th. Now I need to figure out the first date in that week (hence November 2nd) before grabbing the rest of the days and this is where everything gets messed up for me. This is what I've come up with:
' getyear = 2014, getweek = 45
Dim DateOfFirstWeekDay As DateTime = GetDateOfFirstDayOfWeek(getyear, getweek)
Dim FirstDateInSequence As DateTime = CDate(DateAdd("d", _
CInt(Abs(Integer.Parse(Weekday(DateOfFirstWeekDay, WeekStartsWith))) * -1) + 1, _
DateOfFirstWeekDay)).ToShortDateString()
Protected Friend Shared Function GetDateOfFirstDayOfWeek(ByVal getyear As Nullable(Of Integer), _
ByVal getweek As Nullable(Of Integer)) As DateTime
Dim firstWeekDay As DateTime = GetFirstDayOfWeek(newYearDay)
If getweek = 1 Then
getweek -= 1
End If
Return DateAdd(DateInterval.WeekOfYear, CInt(getweek), firstWeekDay)
End Function
Protected Friend Shared Function GetFirstDayOfWeek(ByVal dt As DateTime) As DateTime
If dt.DayOfWeek = DayOfWeek.Sunday Then
Return dt.AddDays(-6)
Else
Return dt.AddDays(1 - CInt(dt.DayOfWeek))
End If
End Function
As my question implies November 2nd is not the result I get. Instead FirstDateInSequence returns December 22, 2013 when I input 2014/45. It's pretty safe to assume something fails me here. I just can't get my head around it. I'd like your point of view to this. Where should I focus my attention in the code above?
I'm having a hard time quickly following your code logic. So here's mine.
You could start by finding the first day of the first week of that year
Dim d As New DateTime(year, 1, 1)
d = d.AddDays(-d.DayOfWeek)
And then add the number of days (week_number -1) * 7
d = d.AddDays((week_number - 1) * 7)
I do a -1 since I assume that week_number will be equal to 1 to get the first week. Since d is already equal to the first week, we start counting at 0.
To get the last day, just add 6 (or 7) days to the result

Out of a set of data returned grouped by months, only the initial month is returning incorrect data

I am trying to return data for the last 24 months as of the end of last month. Only the initial month returns incorrect data, while all the other months are correct. I believe the issue is in the strBeginDate = section of the code. Any ideas what would be causing partially returned data for the initial month? Thank you very much.
Sub GetStaticApprovalRates_Slide6()
Dim strBeginDate
Dim strEndDate
strEndDate = Sheets("Instructions").Range("EndDate").Value
strBeginDate = DateAdd("m", -23, strEndDate) + 1
Sheets("Slide6Data").Select
It's hard to say exactly what's wrong based only on what you posted. But I do see that you are calculating the start date based on the end date, by only subtracting months. There is no allowance for days. So you might be missing some of that first month by not allowing for the early days of that first month.
That is , if end date occurs mid-month, I think your algorithm would cause start date to be mid-month also. Perhaps missing days 1-x of that first month.

Find first and last day for previous calendar month in SQL Server Reporting Services (VB.Net)

I'm creating a report in MS SQL Server Reporting Services and need to set the default Start and End Date report parameters to be the first and last dates of the previous calendar month and need help.
The report is generated on the 2nd calendar day of the month and I need values for:
Previous Calendar Month
- first day
- last day
I've been working with DateAdd, but have not been successful at creating an Expression (in VB.NET as I understand it). I would really appreciate any help you can give me!
Randall, here are the VB expressions I found to work in SSRS to obtain the first and last days of any month, using the current month as a reference:
First day of last month:
=dateadd("m",-1,dateserial(year(Today),month(Today),1))
First day of this month:
=dateadd("m",0,dateserial(year(Today),month(Today),1))
First day of next month:
=dateadd("m",1,dateserial(year(Today),month(Today),1))
Last day of last month:
=dateadd("m",0,dateserial(year(Today),month(Today),0))
Last day of this month:
=dateadd("m",1,dateserial(year(Today),month(Today),0))
Last day of next month:
=dateadd("m",2,dateserial(year(Today),month(Today),0))
The MSDN documentation for the VisualBasic DateSerial(year,month,day) function explains that the function accepts values outside the expected range for the year, month, and day parameters. This allows you to specify useful date-relative values. For instance, a value of 0 for Day means "the last day of the preceding month". It makes sense: that's the day before day 1 of the current month.
These functions have been very helpful to me - especially in setting up subscription reports; however, I noticed when using the Last Day of Current Month function posted above, it works as long as the proceeding month has the same number of days as the current month. I have worked through and tested these modifications and hope they help other developers in the future:
Date Formulas:
Find the First Day of Previous Month:
DateAdd("m", -1, DateSerial(Year(Today()), Month(Today()), 1))
Find Last Day of Previous Month:
DateSerial(Year(Today()), Month(Today()), 0)
Find First Day of Current Month:
DateSerial(Year(Today()),Month(Today()),1)
Find Last Day of Current Month:
DateSerial(Year(Today()),Month(DateAdd("m", 1, Today())),0)
Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
Dim firstDayLastMonth As DateTime
Dim lastDayLastMonth As DateTime
firstDayLastMonth = thisMonth.AddMonths(-1)
lastDayLastMonth = thisMonth.AddDays(-1)
I'm not familiar with SSRS, but you can get the beginning and end of the previous month in VB.Net using the DateTime constructor, like this:
Dim prevMonth As DateTime = yourDate.AddMonths(-1)
Dim prevMonthStart As New DateTime(prevMonth.Year, prevMonth.Month, 1)
Dim prevMonthEnd As New DateTime(prevMonth.Year, prevMonth.Month, DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month))
(yourDate can be any DateTime object, such as DateTime.Today or #12/23/2003#)
in C#:
new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1)
new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1)
I was having some difficulty translating actual VB.NET to the Expression subset that SSRS uses. You definitely inspired me though and this is what I came up with.
StartDate
=dateadd("d",0,dateserial(year(dateadd("d",-1,dateserial(year(Today),month(Today),1))),month(dateadd("d",-1,dateserial(year(Today),month(Today),1))),1))
End Date
=dateadd("d",0,dateserial(year(Today),month(Today),1))
I know it's a bit recursive for the StartDate (first day of last month). Is there anything I'm missing here? These are strictly date fields (i.e. no time), but I think this should capture leap year, etc.
How did I do?
I was looking for a simple answer to solve this myself. here is what I found
This will split the year and month, take one month off and get the first day.
firstDayInPreviousMonth = DateSerial(Year(dtmDate), Month(dtmDate) - 1, 1)
Gets the first day of the previous month from the current
lastDayInPreviousMonth = DateSerial(Year(dtmDate), Month(dtmDate), 0)
More details can be found at:
http://msdn.microsoft.com/en-us/library/aa227522%28v=vs.60%29.aspx
This one will give you date no time:
=FormatDateTime(DateAdd("m", -1, DateSerial(Year(Today()), Month(Today()), 1)),
DateFormat.ShortDate)
This one will give you datetime:
=dateadd("m",-1,dateserial(year(Today),month(Today),1))
Dim aDate As DateTime = #3/1/2008# 'sample date
Dim StartDate As DateTime = aDate.AddMonths(-1).AddDays(-(aDate.Day - 1))
Dim EndDate As DateTime = StartDate.AddDays(DateTime.DaysInMonth(StartDate.Year, StartDate.Month) - 1)
'to access just the date portion
' StartDate.Date
' EndDate.Date