Number of days (as integer) between two dates in Grid

3 posts, 0 answers
  1. Mark
    Mark avatar
    11 posts
    Member since:
    Jun 2012

    Posted 08 Nov 2012 Link to this post

    I have a RadGrid with two date columns and I have added a Calculated Column to get the number of days between them.

    I tried a couple of techniques I found from Google to display the number of days but it always displays in this format:  2.00:00:00 for two days.  Fourty days looks like 40.00:00:00.  I just want to display the number of days as an integer like, "2" or "40".

    The Calculated column UniqueName="DaysOpen".

    One of the techniques I tried did the calculation in the .vb file and was supposed to send the value to a GridTemplateColumn with a <asp:Label /> in the ItemTemplate, but that method didn't work at all.  The column showed no values at all.  The column was there but all the fields were blank.
    .aspx
            <telerik:GridCalculatedColumn DataFields="DateResolved,DateReceived" Expression="{0}-{1}" headerstyle-width="50px"
                HeaderText="Days Open" UniqueName="DaysOpen"  Aggregate="None">
            </telerik:GridCalculatedColumn>
     
     
    <%--
            <telerik:GridTemplateColumn UniqueName="Temp" HeaderStyle-Width="50px" HeaderText="days open">
                <ItemTemplate>
                  <asp:Label ID="DaysOpen" runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
    --%>


    .aspx.vb  -  I commented out both methods because neither of them seemed to do anything.  In or out made no difference to the grid contents at all like they just weren't accessing the grid objects, which is probably the case.  On line 27 I hard coded a "3" in place of the now commented out, [date].ToString and the column was still blank.
    Imports Telerik.Web.UI
    Partial Class test
        Inherits System.Web.UI.Page
     
        'Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        '    'grid.MasterTableView.SwapColumns("ComplaintID", "DateReceived")
        'End Sub
     
        'Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        '    If TypeOf e.Item Is GridDataItem Then
        '        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
        '        Dim myTS As TimeSpan = TimeSpan.Parse(item("DaysOpen").Text)
        '        item("DaysOpen").Text = [String].Format("{0}h {1}m {2}s", myTS.Days, myTS.Hours, myTS.Minutes)
        '    End If
        'End Sub
     
        '  Both methods of getting the difference between two dates work equally well.
        '  Actually neither of them seem to do shit. When I commented them both out I still got the same correct results, but formatted like this:  2.00:00:00 for two days.
     
        'Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        '    If TypeOf e.Item Is GridDataItem Then
        '        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
        '        Dim DaysOpen As Label = TryCast(item.FindControl("DaysOpen"), Label)
        '        Dim DateResolved As String = (item("DateResolved").Text)
        '        Dim DateReceived As String = (item("DateReceived").Text)
        '        Dim [date] As Integer = Convert.ToDateTime(DateResolved).Subtract(Convert.ToDateTime(DateReceived)).Days
        '        DaysOpen.Text = "3" ' [date].ToString()
        '    End If
        'End Sub
     
    End Class

    With my markup and code just as above the DaysOpen column displayes the correct number of days, just in the long format.  I just want to display it as an integer. e.g. "40" not "40.00:00:00"

    Thank you!
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 09 Nov 2012 Link to this post

    Hello,

    Please try with below code snippet.
    <telerik:GridCalculatedColumn DataFields="DateResolved,DateReceived Expression="{0}-{1}"
                      DataFormatString="{0:dd}">


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Meir
    Meir avatar
    1 posts
    Member since:
    Sep 2016

    Posted 15 Sep Link to this post

    this worked for me:

    <sq:GridCalculatedColumn DataFields="fldToDate,fldFromDate" Expression="({0}-{1}).Value.Ticks/864000000000+1" HeaderText="Private Days" UniqueName="fldPrivateDays" DataType="System.Int32" Aggregate="Sum" ></sq:GridCalculatedColumn>

Back to Top