Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

Add a day to the date range

I am working on Date Range Picker where a user can pick specific date and according to the date range (parameter passed) he will be displayed the report. Right now everything works fine untill a user chooses his start date and end date as the same day. eg start date 10/06/2008 and enddate 10/06/2008, the report then errors out.

I want to insert a logic below, if the SelectedBeginDate = SelectedEndDate. I want SelectedEndDate added by a day. Say a user chooses start date 10/06/2008 and enddate 10/06/2008 ..the end date should be converted as 10/07/2008..I have been trying this for couple of hours but in vain...How do i do this?..please advice
Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        Else
            If (IsNothing(dateRange.SelectedBeginDate) And Not IsNothing(dateRange.SelectedEndDate)) Or _
               (Not IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
                Return False
            End If
        End If
        Return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedBeginDate.Value.Date
    End Function

Open in new window

0
Kerau
Asked:
Kerau
  • 19
  • 13
  • 5
  • +1
3 Solutions
 
Kevin CrossChief Technology OfficerCommented:
Try something like this:
(since that is a custom component I don't have -- I couldn't test, but this should work for date objects)
Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        ElseIf dateRange.SelectedEndDate.Value.Date = dateRange.SelectedBeginDate.Value.Date Then '<<< added
            'May need to use this in above IF
            'DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0
            dateRange.SelectedEndDate.Value.Date = DateAdd(DateInterval.Day, 1, dateRange.SelectedBeginDate.Value.Date)
        End If
 
        Return dateRange.SelectedEndDate.Value.Date > dateRange.SelectedBeginDate.Value.Date '<<< changed
    End Function

Open in new window

0
 
David RobitailleAnalyst ProgrammerCommented:
use the AddDays function
dateRange.SelectedEndDate.Value.Date.AddDays(1)

0
 
Kevin CrossChief Technology OfficerCommented:
Great suggestion, davrob60.

Again this works if these are ultimately Date objects.
Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        ElseIf dateRange.SelectedEndDate.Value.Date = dateRange.SelectedBeginDate.Value.Date Then '<<< added
            'May need to use this in above IF
            'DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0
            dateRange.SelectedEndDate.Value.Date.AddDays(1)
        End If
 
        Return dateRange.SelectedEndDate.Value.Date > dateRange.SelectedBeginDate.Value.Date '<<< changed
    End Function

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
KerauAuthor Commented:
Thanks for a quick response. But it says "Expression is a value and therefore can't be the target of an assignment."
0
 
JohnP_RealiniCommented:
Hi...
This is what we do in similar situations..
First: determine what is the starting hour of the date picked to get the report (i.e.: if the user chooses 10/07 does he intend to watch operations of that day beginning at 6 a.m.? ) So, when the date is chosen, you should add "06:00:00" to that date ( date = date + "06:00:00 a.m.") where date is just date (I mean, date is "10/07/2008")
Then... when he chooses the ending date, I suppose it means he wants to have all operations of that date too... So you need to determine what is the closing hour for the ending date operations. In the same way, you add to this date the closing hour (date = date + "08:00:00 p.m."

This way... if he chooses the same date for starting and ending, you will have a report of all operations from 6 a.m. to 8 p.m.

Tell me if you don't know how to modify the date variables... hope this helps
0
 
JohnP_RealiniCommented:
Forgot to mention something... forget the "daterange".. use two datepicker controls... one for starting date, one for ending date, and assign values to separate variables... that will make it much easier to handle
0
 
Kevin CrossChief Technology OfficerCommented:
See my comments in code.  You may want this:

...
ElseIf DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0 Then
...
0
 
KerauAuthor Commented:
doesnot work :(. The code works fine but the validation comes into picture when i enter same date range, which i don't want. So, we have to keep "=" sign without the quotes.

Return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedBeginDate.Value.Date

And if i keep the = sign it doesn't give me the report desired..

For your clarification i want to add a date to allow a user enter 10/07/2008 to 10/07/2008 to let him see the report from 12:00 AM to 11:59:59PM of the very day
 Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        ElseIf DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0 Then '<<< added
            'May need to use this in above IF
            'DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0
 
            dateRange.SelectedEndDate.Value.Date.AddDays(1)
        End If
 
        Return dateRange.SelectedEndDate.Value.Date > dateRange.SelectedBeginDate.Value.Date '<<< changed
    End Function

Open in new window

0
 
David RobitailleAnalyst ProgrammerCommented:
try this

dateRange.SelectedBeginDate.value.Date.AddDays(1).AddSeconds(-1)
0
 
David RobitailleAnalyst ProgrammerCommented:
sorry it was SelectedEndDate.
but you know what i meen. it add a day and substract a second so tou got 23:59:59 that very day
0
 
KerauAuthor Commented:
Really appreciate your efforts, I selected my begin date 10/01/2008 and end date as 10/01/2008..Shouldn't the value shown in the screenshot would change to 10/02/2008. :(



hi.JPG
0
 
Kevin CrossChief Technology OfficerCommented:
No because you subtracted a second to make it 10/1/2008 23:59:59
0
 
KerauAuthor Commented:
okay then the issue would be with the report ... the report will not run...i am sorry if i sound same so how do i make it take param..if the user inputs start date 10/01/2008 to 10/01/2008...
this should be converted as10/01/2008 to 10/02/2008...
0
 
David RobitailleAnalyst ProgrammerCommented:
no it becaut it a function. so you should set it some were or call it directly, isnce it remove a second, you dont need the datediff thing
 

return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedEndDate.value.Date.AddDays(1).AddSeconds(-1)

Open in new window

0
 
David RobitailleAnalyst ProgrammerCommented:
something like this
Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        else
            Return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedEndDate.value.Date.AddDays(1).AddSeconds(-1)         End If
 
    End Function

Open in new window

0
 
KerauAuthor Commented:
well it says ..invalid date range...wierd
0
 
David RobitailleAnalyst ProgrammerCommented:
my error, i missplaced the end and start


Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        else
            Return dateRange.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1) >= dateRange.SelectedEndDate.value.Date
       End If
 
    End Function

Open in new window

0
 
KerauAuthor Commented:
used the exact same code and no results. It doesn't add in a day to the selected date...when i started this, i thought it would have been much easier to achieve....:(
0
 
KerauAuthor Commented:
this is the format i am looking for if the report param date is 10/01/2008 to 10/01/2008

i want to see the report of 10/1/2008 12:00:00AM To 10/2/2008 12:00:00AM
0
 
David RobitailleAnalyst ProgrammerCommented:
ok, it<s because your function just test the daye and return a boolean.
The Date Range Picker is not modified by the validation.
As you said, you got the error "Expression is a value and therefore can't be the target of an assignment." when you try to modify the dateRange s values...

you could try dateRange.SelectedEndDate = dateRange.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1)
if it dont work,in the part where you made the select, you should try to add the dateRange.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1)
For more help. please provide the part where you select the data
0
 
Kevin CrossChief Technology OfficerCommented:
Good catch...Wasn't paying attention to that after got going.

The reason I changed this in my original posting:
Return dateRange.SelectedEndDate.Value.Date > dateRange.SelectedBeginDate.Value.Date '<<< changed

What that you want to return false if the end date is <= start and then set end to start + 1 day.  Even works if null as you can test start for isnothing and set to today's date then do what was suggested above:
>>you could try dateRange.SelectedEndDate.Value.Date = dateRange.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1)
0
 
KerauAuthor Commented:
So is this the place i need to make necessary changes



Protected Overridable Sub AddEndDateParameter(ByVal Report As CrystalDecisions.ReportAppServer.ClientDoc.ReportClientDocument)
        Report.DataDefController.ParameterFieldController.SetCurrentValues(String.Empty, Report.DataDefinition.ParameterFields(1).Name, GetSingleValueParameterFieldValues(HttpContext.Current.Request.Params("dteEnd")))
    End Sub

Open in new window

0
 
David RobitailleAnalyst ProgrammerCommented:
what is the data type of HttpContext.Current.Request.Params("dteEnd")?
cause you could try
ctype(HttpContext.Current.Request.Params("dteEnd"), date).AddDays(1).AddSeconds(-1)
0
 
KerauAuthor Commented:
FYI : Public member 'AddDays' on type 'ValuesClass' not found.
0
 
KerauAuthor Commented:
This might not be relevant at all but can we write something on button click :)
0
 
David RobitailleAnalyst ProgrammerCommented:
oops
ctype(GetSingleValueParameterFieldValues(HttpContext.Current.Request.Params("dteEnd"), date).AddDays(1).AddSeconds(-1).tostring()
?
0
 
KerauAuthor Commented:
or on the validation page itself, something like this

  Public Overrides Sub Clear()
        'drpReportDates.SelectedBeginDate = DateTime.Now
        drpReportDates.SelectedEndDate = Add date here'DateTime.Now
       
    End Sub
0
 
David RobitailleAnalyst ProgrammerCommented:
here you go
drpReportDates.SelectedEndDate = DateTime.Now.Date.AddDays(1).AddSeconds(-1)
0
 
KerauAuthor Commented:
okay tried you above method and says "Date is a type and can't be used as an expression.
0
 
KerauAuthor Commented:
I have something like this

Protected Sub ValidateEndDate(ByVal sender As Object, ByVal e As ServerValidateEventArgs) Handles cvValidEndDate.ServerValidate
        e.IsValid = EndDateIsValid(drpReportDates)
    End Sub
0
 
David RobitailleAnalyst ProgrammerCommented:
but, what are you trying to do? why setting the drpReportDates.SelectedEndDate in the Sub Clear()???
you chould realy try to set it in the so it will be passed to you select function
Report.DataDefController.ParameterFieldController.SetCurrentValues(
 
It dont work this way???
0
 
KerauAuthor Commented:
nope!!!

Here is the whole page ..if you r interested
Partial Class Secure
    Inherits BaseWebFormPage
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        rfvReport.Text = MessageRegistry.Message("FieldErrorIndicatorString").ToString + " " + _
                         MessageRegistry.Message("InputRequiredError").ToString
        csvRequiredEventDate.Text = MessageRegistry.Message("FieldErrorIndicatorString").ToString + " " + _
                                    MessageRegistry.Message("InputRequiredError").ToString
        cvValidDateRange.Text = MessageRegistry.Message("FieldErrorIndicatorString").ToString + " " + _
                                MessageRegistry.Message("InputInvalidDateRangeError")
        cvValidEndDate.Text = MessageRegistry.Message("FieldErrorIndicatorString").ToString + " " + _
                               MessageRegistry.Message("EndDateGreaterThanTodayError").ToString()
 
    End Sub
 
    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.ddlReports.SelectedIndex = -1
        Me.drpReportDates.SelectedEndDate = Nothing
        Me.drpReportDates.SelectedBeginDate = Nothing
    End Sub
 
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If Page.IsValid Then
            Dim urlString As String = ""
            Select Case Me.ddlReports.SelectedValue
                Case "abc.aspx"
                    urlString = String.Format("abc.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.ToShortDateString)
                Case "bcd.aspx"
                    urlString = String.Format("bcd.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.ToShortDateString)
            End Select
            Dim scriptString As New StringBuilder
            scriptString.Append("<script language=JavaScript>")
            scriptString.Append("window.open('" + urlString + "')")
            scriptString.Append("</script>")
            If (Not ClientScript.IsStartupScriptRegistered("Startup")) Then
                ClientScript.RegisterStartupScript(GetType(Page), "Startup", scriptString.ToString)
            End If
        End If
    End Sub
 
    Protected Sub ddlReports_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlReports.SelectedIndexChanged
        trDates.Visible = True
    End Sub
 
    Protected Sub ValidateEndDate(ByVal sender As Object, ByVal e As ServerValidateEventArgs) Handles cvValidEndDate.ServerValidate
        e.IsValid = EndDateIsValid(drpReportDates)
    End Sub
 
    Protected Overridable Function EndDateIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If Not IsNothing(dateRange.SelectedEndDate) And Not IsNothing(dateRange.SelectedBeginDate) Then
            If dateRange.SelectedEndDate.Value.Date > DateTime.Now.Date Then
                Return False
            End If
        End If
 
        Return True
    End Function
 
    Protected Sub RequireEventDate(ByVal sender As Object, ByVal e As ServerValidateEventArgs) Handles csvRequiredEventDate.ServerValidate
        e.IsValid = DateRangeIsNotNull(drpReportDates)
    End Sub
 
    Protected Overridable Function DateRangeIsNotNull(ByVal dateRange As DateRangePicker) As Boolean
        Return _
         Not IsNothing(dateRange.SelectedBeginDate) AndAlso _
         Not IsNothing(dateRange.SelectedEndDate)
    End Function
 
    Protected Sub ValidateDateRange(ByVal sender As Object, ByVal e As ServerValidateEventArgs) Handles cvValidDateRange.ServerValidate
        e.IsValid = DateRangeIsValid(drpReportDates)
    End Sub
 
    Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        Else
            If (IsNothing(dateRange.SelectedBeginDate) And Not IsNothing(dateRange.SelectedEndDate)) Or _
               (Not IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
                Return False
            End If
        End If
        Return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedBeginDate.Value.Date
    End Function
 
End Class

Open in new window

0
 
KerauAuthor Commented:
can i just add it here

urlString = String.Format("bcd.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.ToShortDateString)
0
 
KerauAuthor Commented:
So when i do something like the following i get the following error..Date is a type and can't be used as an expression...Please suggest





 Protected Overridable Sub AddEndDateParameter(ByVal Report As CrystalDecisions.ReportAppServer.ClientDoc.ReportClientDocument)
        Report.DataDefController.ParameterFieldController.SetCurrentValues(String.Empty, Report.DataDefinition.ParameterFields(1).Name, ctype(GetSingleValueParameterFieldValues(HttpContext.Current.Request.Params("dteEnd"),date).AddDays(1).AddSeconds(-1).tostring()
    End Sub

Open in new window

0
 
David RobitailleAnalyst ProgrammerCommented:
well we will try to do it on the btnSubmit_Click
 urlString = String.Format("bcd.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1).ToShortDateString)

0
 
KerauAuthor Commented:
Pasted exact same code but in vain ...doesn't add a day to  the end date :(...what can i possibly do to make this work...
0
 
David RobitailleAnalyst ProgrammerCommented:
it was because of the ToShortDateString
bu instead, what if we remove the AddSeconds(-1) ?

urlString = String.Format("bcd.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.Date.AddDays(1).ToShortDateString)

0
 
KerauAuthor Commented:
Davrob60 and all experts who answered this, I really have no clue how you do this, without even having much information..but you are a saviour. This works and works like a charm.,,,,,,,,yhoooooo!!!!I am highly appreciate of you being taking out much of your precious time and trying to answer my question (at times stupid :)). Thanks a lot .. Thanks to mwvisa, JohnP_Realini

Davrob you deserve more than 350 points ..so please post a link of this question to one of my other post http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23791451.html so that i can grant you points as this answers my other question as well.
0
 
KerauAuthor Commented:
my next question http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23829881.html might just interest you all..

Thanks
kerau
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 19
  • 13
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now