I have an asp.net gridview bound to the following sql server 2008r2 data source:

<asp:SqlDataSource ID="dsFault" runat="server" DataSourceMode="DataSet" 
 ConnectionString="<%$ ConnectionStrings:ESBExceptionDb %>"
SelectCommand="select * from Fault order by datetime desc"></asp:SqlDataSource>

In my Fault table there is a column called DateTime of type datetime. The values stored in this column are in UTC format and I need them to be displayed in the local timezone for the browser

I have added a template field to the Columns collection of the grid view as follows:

<asp:TemplateField>
  <ItemTemplate>
   <asp:Label ID="lblLocalTime" runat="server" 
    Text='<%# String.Format("{0:f}", Eval("DateTime").ToLocalTime()) %>'>
   </asp:Label>
  </ItemTemplate>    
</asp:TemplateField>

When I browse the page I get the following error:

CS1061: 'object' does not contain a definition for 'ToLocalTime' and no
extension method 'ToLocalTime' accepting a first argument of type 'object' 
could be found (are you missing a using directive or an assembly reference?)

Can anyone please tell me where I've gone wrong?

Thanks, Rob.

有帮助吗?

解决方案

The Eval("DateTime") Value that returns from your database is not a C# DataTime object.

and because the function .ToLocalTime() belongs to the DateTime c# object you can't use it.

You need to convert the object to string and then use the function .ToLocalTime()

<asp:TemplateField>
  <ItemTemplate>
   <asp:Label ID="lblLocalTime" runat="server" 
    Text='<%# Convert.ToDateTime(Eval("DateTime")).ToLocalTime() %>'>
   </asp:Label>
  </ItemTemplate>    
</asp:TemplateField>

Ones you converted it to DateTime Object, you can use any format available

For Example

Text='<%# Convert.ToDateTime(Eval("DateTime")).ToString("dd/MM/yyyy") %>'

其他提示

In addition to the other answers, remember that ToLocalTime() will return the SERVER local time not the browser local time as you requested.

Try like this

<%# Eval("DateTime", "{0:T}")%>

<asp:Label ID="lblLocalTime" runat="server" 
    Text='<%# Eval("DateTime", "{0:T}")%>'>
   </asp:Label>

This will do the trick! Give this a try.

<asp:TemplateField HeaderText="Account Created">
   <ItemTemplate>
      <asp:Label ID="lblLocalTime" runat="server" 
         Text='<%# Convert.ToDateTime(Eval("CreateDate", "{0:g}")).ToLocalTime() %>'>
      </asp:Label>
   </ItemTemplate>    
</asp:TemplateField>

It looks like there is no clean and simple way to do this, strange!!. The answer's mentioned so far try to convert the date to server timezone, not to client/browser's. i.e if the server is in USA and client in India, you won't see the datetime in indian timezone, it will be in USA's.

A duplicate of this has better answers. Basically, you either have to do one of below -

  • Send client timezone information to the server and do approp conversion there. It is better to send the timezone name rather than timezone offset as timezone offset can differ based on DST. You can use Intl.DateTimeFormat().resolvedOptions().timeZone to send the timezone name.
  • Send UTC date strings to the client and let it do the conversion using javascript(new Date(ISOUTCDateString)). This is what happens in WCF.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top