Raja
Raja

Reputation: 792

VB.NET DateTimePicker range

Or I cannot explain what should be the appropriate question for this.... however...

Hi,

I am developing a tax software. I have two DateTimePicker controls.

First one is From and the second one is to i.e. Date From and Date to (not exactly...should be month span).

I need to input in the first DateTimePicker and the second one will get the automatic range.

DtpTo.Value = DtpFrom.Value.AddMonths(3)

Up to this is ok. But now comes the complex thing.

If the user inputs 01/04/2016 in the first, the second DateTimePicker will automatically calculate days and display 30/06/2016. If the user inputs 30/06/2016 in the first, the second DateTimePicker must display 30/06/2016.

i.e. the range will be from April to June, July to September and so on. The restriction is whichever date user inputs in the first DateTimePicker control, any code should detect the range and restricts the second DateTimePicker to the end of the day of that range.

Like.....

Example

How should I do this ?

Upvotes: 0

Views: 1479

Answers (3)

Abdellah OUMGHAR
Abdellah OUMGHAR

Reputation: 3745

To get Date To (the last day of the last month of the quarter start date) :

1- Must firstly calculate the quarter of Date from with this function

Math.Ceiling(dateFrom.Month / 3.0)

Math.Ceiling Returns the smallest integer greater than or equal to the specified number. Note the function use 3.0 to perform a floating point division and not an integer division

2- Get last month in trimester by increase the trimster number by 3.

3- Get last day of that quarter's last month

Private Sub DtpFromDtpTo_ValueChanged(sender As Object, e As EventArgs) Handles DtpFromDtpTo.ValueChanged

    Dim dateFrom As Date = DtpFrom.Value.Date
    Dim lastMonthInTrimester As Integer = Math.Ceiling(dateFrom.Month / 3.0) * 3
    DtpTo.Value = New DateTime(dateFrom.Year, lastMonthInTrimester, DateTime.DaysInMonth(dateFrom.Year, lastMonthInTrimester))

End Sub

You can also get the last month of trimester by:

Dim lastMonthInTrimester As Integer = Convert.ToInt16((dateFrom.Month + 1) / 3) * 3

Upvotes: 1

Suleyman DOGAN
Suleyman DOGAN

Reputation: 185

 Private Sub DtpFrom_ValueChanged(sender As Object, e As EventArgs) Handles DtpFrom.ValueChanged
    Dim datefrom As Date = DtpFrom.Value
    Dim dateto As Date = Nothing
    If datefrom.AddMonths(3).Month > 6 And datefrom.Month <= 6 Then
        Dim DaysInMonth As Integer = Date.DaysInMonth(datefrom.Year, 6)
        Dtpto.Value = New Date(datefrom.Year, 6, DaysInMonth)
    ElseIf datefrom.AddMonths(3).Month <= 6 And datefrom.Month <= 6 Then
        Dtpto.Value = datefrom.AddMonths(3)
    ElseIf datefrom.AddMonths(3).Month <= 12 And datefrom.AddMonths(3).Month >= 6 And datefrom.Month >= 6 Then
        Dtpto.Value = datefrom.AddMonths(3)
    ElseIf datefrom.AddMonths(3).Month <= 2 And datefrom.AddMonths(3).Month And datefrom.Month >= 6 Then
        Dim DaysInMonth As Integer = Date.DaysInMonth(datefrom.Year, 12)
        Dtpto.Value = New Date(datefrom.Year, 12, DaysInMonth)
    End If



End Sub

Upvotes: 0

Andrew Morton
Andrew Morton

Reputation: 25013

You need to examine the "from" month and figure out what the last month of the quarter is from that. Then you need the last day of that quarter's last month:

Sub DtpFrom_ValueChanged(sender As Object, e As EventArgs) Handles DtpFrom.ValueChanged
    Dim dtp = DirectCast(sender, DateTimePicker)
    Dim d = dtp.Value.Date

    Dim maxMonth As Integer
    Select Case d.Month
        Case 1 To 3
            maxMonth = 3
        Case 4 To 6
            maxMonth = 6
        Case 7 To 9
            maxMonth = 9
        Case Else
            maxMonth = 12
    End Select

    DtpTo.Value = New Date(d.Year, maxMonth, DateTime.DaysInMonth(d.Year, maxMonth))

End Sub

If it is possible that a quarter includes a year transition, it will be a bit more fiddly.

Upvotes: 3

Related Questions