Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add a day to the date range

Posted on 2008-10-08
39
306 Views
Last Modified: 2012-05-05
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
Comment
Question by:Kerau
  • 19
  • 13
  • 5
  • +1
39 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22668877
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22668904
use the AddDays function
dateRange.SelectedEndDate.Value.Date.AddDays(1)

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22668935
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:Kerau
ID: 22668940
Thanks for a quick response. But it says "Expression is a value and therefore can't be the target of an assignment."
0
 
LVL 3

Assisted Solution

by:JohnP_Realini
JohnP_Realini earned 50 total points
ID: 22668951
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
 
LVL 3

Expert Comment

by:JohnP_Realini
ID: 22668975
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
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22668977
See my comments in code.  You may want this:

...
ElseIf DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0 Then
...
0
 

Author Comment

by:Kerau
ID: 22669147
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22669274
try this

dateRange.SelectedBeginDate.value.Date.AddDays(1).AddSeconds(-1)
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22669293
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
 

Author Comment

by:Kerau
ID: 22669428
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
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22669491
No because you subtracted a second to make it 10/1/2008 23:59:59
0
 

Author Comment

by:Kerau
ID: 22669599
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22669601
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22669629
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
 

Author Comment

by:Kerau
ID: 22669695
well it says ..invalid date range...wierd
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22669737
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
 

Author Comment

by:Kerau
ID: 22669833
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
 

Author Comment

by:Kerau
ID: 22669914
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22669925
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
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 100 total points
ID: 22669991
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
 

Author Comment

by:Kerau
ID: 22670111
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22670193
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
 

Author Comment

by:Kerau
ID: 22670324
FYI : Public member 'AddDays' on type 'ValuesClass' not found.
0
 

Author Comment

by:Kerau
ID: 22670350
This might not be relevant at all but can we write something on button click :)
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22670373
oops
ctype(GetSingleValueParameterFieldValues(HttpContext.Current.Request.Params("dteEnd"), date).AddDays(1).AddSeconds(-1).tostring()
?
0
 

Author Comment

by:Kerau
ID: 22670393
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22670446
here you go
drpReportDates.SelectedEndDate = DateTime.Now.Date.AddDays(1).AddSeconds(-1)
0
 

Author Comment

by:Kerau
ID: 22670456
okay tried you above method and says "Date is a type and can't be used as an expression.
0
 

Author Comment

by:Kerau
ID: 22670503
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22670510
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
 

Author Comment

by:Kerau
ID: 22670553
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
 

Author Comment

by:Kerau
ID: 22670613
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
 

Author Comment

by:Kerau
ID: 22670733
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22670812
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
 

Author Comment

by:Kerau
ID: 22670945
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
 
LVL 18

Accepted Solution

by:
David Robitaille earned 350 total points
ID: 22670994
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
 

Author Closing Comment

by:Kerau
ID: 31504219
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
 

Author Comment

by:Kerau
ID: 22758742
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question