# how to calculate Time Diffrence between Two columns in Sql Using Asp.net [Answered]RSS

## 3 replies

Last post Aug 02, 2016 01:09 PM by DDNaidu

Member

35 Points

76 Posts

### how to calculate Time Diffrence between Two columns in Sql Using Asp.net

How to calculate Time Diffrence Between Two columns in sql

I want the result like:

7hours 15min like this....

how to i write this Query..?? and

How to i show it in Gridview With Hours and Minitus..??

Contributor

2135 Points

2145 Posts

### Re: how to calculate Time Diffrence between Two columns in Sql Using Asp.net

You need to calculate what is called the TIMESPAN, it is a type and when both of your time values are properly defined, you can simply request the span between the two like this....

```        Dim dt1 As DateTime = "10/2/2008 8:05:03 AM"
Dim dt2 As DateTime = "10/1/2008 9:12:03 PM"

Dim tSpan As System.TimeSpan = dt1 - dt2```

Contributor

5751 Points

1399 Posts

### Re: how to calculate Time Diffrence between Two columns in Sql Using Asp.net

Aug 02, 2016 12:41 PM|Brando ZWZ|LINK

Hi DDNaidu,

#### DDNaidu

I want the result like:

7hours 15min like this....

how to i write this Query..?? and

According to your description, I suggest you could use 'DATEDIFF' method in sql to calculate Time Diffrence Between Two columns.

https://msdn.microsoft.com/en-us/library/ms189794.aspx

Besides, if you want to show the result as '7hours 15min ', you could refer to follow codes:

```SELECT
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 / 24 / 7 AS NVARCHAR(50)) + ' weeks '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 / 24 % 7 AS NVARCHAR(50)) + ' days '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 % 24  AS NVARCHAR(50)) + ' hours '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 % 60 AS NVARCHAR(50)) + ' minutes' as Datetime1 from [DateTimeTest]```

#### DDNaidu

How to i show it in Gridview With Hours and Minitus..??

You could refer to follow codes:

```<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DateTimeGrid.aspx.cs" Inherits="Grid.DateTimeGrid" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>

</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%\$ ConnectionStrings:DemoConnectionString %>" SelectCommand="SELECT
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 / 24 / 7 AS NVARCHAR(50)) + ' weeks '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 / 24 % 7 AS NVARCHAR(50)) + ' days '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 / 60 % 24  AS NVARCHAR(50)) + ' hours '
+ CAST(DATEDIFF(second, StartDate, EndDate) / 60 % 60 AS NVARCHAR(50)) + ' minutes' as Datetime1  from [DateTimeTest]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
```

Datetable:

Result:

Best Regards,

Brando

Member

35 Points

76 Posts