date time difference

Discussion in 'ASP .Net' started by bbawa1@yahoo.com, Jun 15, 2007.

  1. Guest

    Hi,

    I have a table which has a field ItemsReceived of type datetime. I
    have a grid view which has two columns.

    In first column i have to show the data from field ItemsReceived and
    in second column I have to show
    difference between Currenttime and date from ItemReceived. How can I
    do that.

    e'g

    ItemRecieved Difference
    6/13/2007 12:38am 1d 21h 45m
    6/13/2007 3:54pm 1d 06h 10m
    6/15/2007 12:26pm 34m

    So the second coulmn displays the difference of current time minus
    ItemRecieved. the format shouild be <mintues>m,
    or<hours>h, <mintues>m, or <days>d.

    How can I do that.

    Thanks in advance
    , Jun 15, 2007
    #1
    1. Advertising

  2. Hi there,

    I prepared an example that you get you on the right track:

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
    gridView.DataSource = GetData();
    gridView.DataBind();
    }

    private System.Data.DataTable GetData()
    {
    System.Data.DataTable table =
    new System.Data.DataTable();

    table.Columns.Add("Id", typeof(int));
    table.Columns.Add("ItemRecieved", typeof(DateTime));
    table.Columns.Add("Difference", typeof(TimeSpan));
    //t.ColumnChanged += new DataColumnChangeEventHandler(t_ColumnChanged);
    table.ColumnChanged +=
    new System.Data.DataColumnChangeEventHandler(table_ColumnChanged);

    for (int i = 0; i < 10; i++)
    {
    System.Data.DataRow row = table.NewRow();
    row[0] = i;
    row[1] = DateTime.Now.AddDays((double)i);
    table.Rows.Add(row);
    }

    return table;
    }

    private void table_ColumnChanged(object sender,
    System.Data.DataColumnChangeEventArgs e)
    {
    if (e.Column.ColumnName == "ItemRecieved")
    {
    DateTime date = (DateTime)e.Row["ItemRecieved"];
    e.Row["Difference"] = DateTime.Now - date;
    }
    }
    </script>

    <asp:GridView runat="server" ID="gridView"
    AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="Id" />
    <asp:BoundField DataField="ItemRecieved" />
    <asp:BoundField DataField="Difference"
    DataFormatString="{0:-d.hh:mm:ss}" />
    </Columns>
    </asp:GridView>

    Hope this helps

    --
    Milosz


    "" wrote:

    > Hi,
    >
    > I have a table which has a field ItemsReceived of type datetime. I
    > have a grid view which has two columns.
    >
    > In first column i have to show the data from field ItemsReceived and
    > in second column I have to show
    > difference between Currenttime and date from ItemReceived. How can I
    > do that.
    >
    > e'g
    >
    > ItemRecieved Difference
    > 6/13/2007 12:38am 1d 21h 45m
    > 6/13/2007 3:54pm 1d 06h 10m
    > 6/15/2007 12:26pm 34m
    >
    > So the second coulmn displays the difference of current time minus
    > ItemRecieved. the format shouild be <mintues>m,
    > or<hours>h, <mintues>m, or <days>d.
    >
    > How can I do that.
    >
    > Thanks in advance
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 16, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 15, 4:16 pm, Milosz Skalecki [MCAD] <>
    wrote:
    > Hi there,
    >
    > I prepared an example that you get you on the right track:
    >
    > <script runat="server">
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > gridView.DataSource = GetData();
    > gridView.DataBind();
    >
    > }
    >
    > private System.Data.DataTable GetData()
    > {
    > System.Data.DataTable table =
    > new System.Data.DataTable();
    >
    > table.Columns.Add("Id", typeof(int));
    > table.Columns.Add("ItemRecieved", typeof(DateTime));
    > table.Columns.Add("Difference", typeof(TimeSpan));
    > //t.ColumnChanged += new DataColumnChangeEventHandler(t_ColumnChanged);
    > table.ColumnChanged +=
    > new System.Data.DataColumnChangeEventHandler(table_ColumnChanged);
    >
    > for (int i = 0; i < 10; i++)
    > {
    > System.Data.DataRow row = table.NewRow();
    > row[0] = i;
    > row[1] = DateTime.Now.AddDays((double)i);
    > table.Rows.Add(row);
    > }
    >
    > return table;
    >
    > }
    >
    > private void table_ColumnChanged(object sender,
    > System.Data.DataColumnChangeEventArgs e)
    > {
    > if (e.Column.ColumnName == "ItemRecieved")
    > {
    > DateTime date = (DateTime)e.Row["ItemRecieved"];
    > e.Row["Difference"] = DateTime.Now - date;
    > }}
    >
    > </script>
    >
    > <asp:GridView runat="server" ID="gridView"
    > AutoGenerateColumns="false">
    > <Columns>
    > <asp:BoundField DataField="Id" />
    > <asp:BoundField DataField="ItemRecieved" />
    > <asp:BoundField DataField="Difference"
    > DataFormatString="{0:-d.hh:mm:ss}" />
    > </Columns>
    > </asp:GridView>
    >
    > Hope this helps
    >
    > --
    > Milosz
    >
    >
    >
    > "" wrote:
    > > Hi,

    >
    > > I have a table which has a field ItemsReceived of type datetime. I
    > > have a grid view which has two columns.

    >
    > > In first column i have to show the data from field ItemsReceived and
    > > in second column I have to show
    > > difference between Currenttime and date from ItemReceived. How can I
    > > do that.

    >
    > > e'g

    >
    > > ItemRecieved Difference
    > > 6/13/2007 12:38am 1d 21h 45m
    > > 6/13/2007 3:54pm 1d 06h 10m
    > > 6/15/2007 12:26pm 34m

    >
    > > So the second coulmn displays the difference of current time minus
    > > ItemRecieved. the format shouild be <mintues>m,
    > > or<hours>h, <mintues>m, or <days>d.

    >
    > > How can I do that.

    >
    > > Thanks in advance- Hide quoted text -

    >
    > - Show quoted text -


    Here I am grabbing Itemrecieved and ID from the tables. I am using
    the following query. In your code where should I use that query to get
    other fields.

    select tck.tcktreceived, tck.ticketid, tckmsg.tcktmessage
    from tbtickets tck inner join tbticketsmessages tckmsg
    on tck.ticketid = tckmsg.ticketid.
    , Jun 16, 2007
    #3
  4. Howdy,

    Pseudocode:

    DataTable table = new DataTable();

    table.Columns.Add("Difference", typeof(TimeSpan));
    table.ColumnChanged +=
    new System.Data.DataColumnChangeEventHandler(table_ColumnChanged);

    using (SqlConnection connection = new SqlConnection(ConnectionString))
    {
    SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
    adapter.Fill(table);
    }

    You should be fine from this point.
    --
    Milosz


    "" wrote:

    > On Jun 15, 4:16 pm, Milosz Skalecki [MCAD] <>
    > wrote:
    > > Hi there,
    > >
    > > I prepared an example that you get you on the right track:
    > >
    > > <script runat="server">
    > >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > gridView.DataSource = GetData();
    > > gridView.DataBind();
    > >
    > > }
    > >
    > > private System.Data.DataTable GetData()
    > > {
    > > System.Data.DataTable table =
    > > new System.Data.DataTable();
    > >
    > > table.Columns.Add("Id", typeof(int));
    > > table.Columns.Add("ItemRecieved", typeof(DateTime));
    > > table.Columns.Add("Difference", typeof(TimeSpan));
    > > //t.ColumnChanged += new DataColumnChangeEventHandler(t_ColumnChanged);
    > > table.ColumnChanged +=
    > > new System.Data.DataColumnChangeEventHandler(table_ColumnChanged);
    > >
    > > for (int i = 0; i < 10; i++)
    > > {
    > > System.Data.DataRow row = table.NewRow();
    > > row[0] = i;
    > > row[1] = DateTime.Now.AddDays((double)i);
    > > table.Rows.Add(row);
    > > }
    > >
    > > return table;
    > >
    > > }
    > >
    > > private void table_ColumnChanged(object sender,
    > > System.Data.DataColumnChangeEventArgs e)
    > > {
    > > if (e.Column.ColumnName == "ItemRecieved")
    > > {
    > > DateTime date = (DateTime)e.Row["ItemRecieved"];
    > > e.Row["Difference"] = DateTime.Now - date;
    > > }}
    > >
    > > </script>
    > >
    > > <asp:GridView runat="server" ID="gridView"
    > > AutoGenerateColumns="false">
    > > <Columns>
    > > <asp:BoundField DataField="Id" />
    > > <asp:BoundField DataField="ItemRecieved" />
    > > <asp:BoundField DataField="Difference"
    > > DataFormatString="{0:-d.hh:mm:ss}" />
    > > </Columns>
    > > </asp:GridView>
    > >
    > > Hope this helps
    > >
    > > --
    > > Milosz
    > >
    > >
    > >
    > > "" wrote:
    > > > Hi,

    > >
    > > > I have a table which has a field ItemsReceived of type datetime. I
    > > > have a grid view which has two columns.

    > >
    > > > In first column i have to show the data from field ItemsReceived and
    > > > in second column I have to show
    > > > difference between Currenttime and date from ItemReceived. How can I
    > > > do that.

    > >
    > > > e'g

    > >
    > > > ItemRecieved Difference
    > > > 6/13/2007 12:38am 1d 21h 45m
    > > > 6/13/2007 3:54pm 1d 06h 10m
    > > > 6/15/2007 12:26pm 34m

    > >
    > > > So the second coulmn displays the difference of current time minus
    > > > ItemRecieved. the format shouild be <mintues>m,
    > > > or<hours>h, <mintues>m, or <days>d.

    > >
    > > > How can I do that.

    > >
    > > > Thanks in advance- Hide quoted text -

    > >
    > > - Show quoted text -

    >
    > Here I am grabbing Itemrecieved and ID from the tables. I am using
    > the following query. In your code where should I use that query to get
    > other fields.
    >
    > select tck.tcktreceived, tck.ticketid, tckmsg.tcktmessage
    > from tbtickets tck inner join tbticketsmessages tckmsg
    > on tck.ticketid = tckmsg.ticketid.
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 16, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Berg
    Replies:
    0
    Views:
    772
    Chris Berg
    Oct 27, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,193
    Michael Borgwardt
    May 30, 2004
  3. Keith Cochrane
    Replies:
    2
    Views:
    597
    Keith Cochrane
    Aug 6, 2006
  4. Summu82
    Replies:
    5
    Views:
    846
    Richard Heathfield
    Jun 7, 2006
  5. Ruwan Budha
    Replies:
    4
    Views:
    1,065
    Kirk Haines
    Mar 9, 2011
Loading...

Share This Page