# Calculating number of seconds given two times

Discussion in 'ASP .Net' started by Asad, Apr 27, 2004.

Hello,

I need to know how I can calculate the number of seconds between two
time stamps, e.g. if I have the following (using ToLongTimeString)

8:28:21 PM

and

9:02:33 PM

How can I find out the number of seconds elapsed between the two
times?

Asad, Apr 27, 2004

2. ### Ken Cox [Microsoft MVP]Guest

Look at the TimeSpan example:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
If Not IsPostBack Then
viewstate("starttime") = Now
Else
Dim tmspan As TimeSpan
tmspan = Date.Now.Subtract _
(CType(viewstate("starttime"), DateTime))
Label1.Text = tmspan.TotalSeconds.ToString
End If
End Sub

"Asad" <> wrote in message
news:...
> Hello,
>
> I need to know how I can calculate the number of seconds between two
> time stamps, e.g. if I have the following (using ToLongTimeString)
>
> 8:28:21 PM
>
> and
>
> 9:02:33 PM
>
> How can I find out the number of seconds elapsed between the two
> times?
>
> Thanks in advance.
>

Ken Cox [Microsoft MVP], Apr 27, 2004

3. ### Matt BertherGuest

Hello ,

I'll presume that you have both times as DateTime objects. If not, you'll need to parse them and then call the Subtract method to get a TimeSpan object.

TimeSpan span = dt.Subtract(dt2);
int seconds = span.Seconds;

> Hello,
>
> I need to know how I can calculate the number of seconds between two
> time stamps, e.g. if I have the following (using ToLongTimeString)
>
> 8:28:21 PM
>
> and
>
> 9:02:33 PM
>
> How can I find out the number of seconds elapsed between the two
> times?
>
> Thanks in advance.
>
>

--

--
Matt Berther
http://www.mattberther.com

Matt Berther, Apr 27, 2004