how do I display 221 seconds as 03:41 using DataFormatString

T

Tarren

Hi:

I am pulling duration out of my DB and it returns duration in seconds. For
the display i would like to show it in time format with colon

So duration of 221 I want it to display 03:41

How do I work with the DataFormatString in my bound column to do this?
Anyone know?

Thanks!
 
E

Eliyahu Goldin

DataFormatString won't help. You should access the cell in either
ItemDataBound or PreRender event and format the value with something like
String.Format (..). You will have to convert the value to an integer, make
minutes and seconds parts and format them as separate decimals.

Eliyahu
 
O

Ollie Riches

use a TimeSpan opbject and pass the '221' as the seconds parameter in the
contructor, you can then use the object to return the number of hours,
minutes, seconds, milliseconds

HTH

Ollie Riches
 
T

Tarren

Sorry about the messed up date - apparently it is not 1/13/2004 - I need
more sleep.

Thanks again for the help.
 
T

Tarren

Hi,

I did it using template columns. Since I bind directly to the reader and
not to a dataset, it was quicker, cleaner to do it in the aspx for my
current app than to do it in the db set.

Thanks all. Hope this helps someone else wanting to do something similar

<asp:TemplateColumn >
<ItemTemplate>
<%# Format((CLng(DataBinder.Eval(Container.DataItem, "duration")) -
(CLng(DataBinder.Eval(Container.DataItem, "duration")) Mod 60 )) / 60,"00")
%>
:
<%# Format(CLng(DataBinder.Eval(Container.DataItem, "duration")) Mod
60,"00") %>
</ItemTemplate>
</asp:TemplateColumn>

221 displays as 03:41 (i wanted the leading zero) :)
 
D

David Jessee

this is a long one....you create a DateTime Instance, using the constructor
that allows you to specify seconds, and then call ToString giving it the
appropriate format string. I've not checked the syntax, but this should be
REALLY close....

<%# ctype(new
DateTime(0,0,0,0,0,ctype(dataitem,DataRowView)("duration")),DateTime).ToStri
ng("mm:ss") %>

...note you don't have to use DataBinder.Eval when you're doing Databinding.
if you're binding to a datatable, the your DataItem is actually a
DataRowView, so you can get the value directly that way as an integer. The
DataBinder always returns Strings...therefore this would work is you wanted
to use the DataBinder Syntax...

<%# ctype(new
DateTime(0,0,0,0,0,Integer.Parse(DataBinder.Eval(Container.DataItem,
"duration"))),DateTime).ToString("mm:ss") %>
 
T

Tarren

thanks! I'll give this one a try too. :)


David Jessee said:
this is a long one....you create a DateTime Instance, using the
constructor
that allows you to specify seconds, and then call ToString giving it the
appropriate format string. I've not checked the syntax, but this should
be
REALLY close....

<%# ctype(new
DateTime(0,0,0,0,0,ctype(dataitem,DataRowView)("duration")),DateTime).ToStri
ng("mm:ss") %>

..note you don't have to use DataBinder.Eval when you're doing
Databinding.
if you're binding to a datatable, the your DataItem is actually a
DataRowView, so you can get the value directly that way as an integer.
The
DataBinder always returns Strings...therefore this would work is you
wanted
to use the DataBinder Syntax...

<%# ctype(new
DateTime(0,0,0,0,0,Integer.Parse(DataBinder.Eval(Container.DataItem,
"duration"))),DateTime).ToString("mm:ss") %>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top