DateTime Column displayed as LocalTime?

6 posts, 0 answers
  1. Paul
    Paul avatar
    31 posts
    Member since:
    Sep 2012

    Posted 01 Dec 2009 Link to this post


    We have an application with SQL Server running in one location and then clients in different timezones using that data.  All datetimes are stored as UTC values.  For display we want them in local date/time.  How do we do that in GridView?

    For example:
    A grid showing a list of events with an updated_date column.  We want those event dates/times as local time.  Can .formatstring be used to somehow set ToLocalTime()?

  2. Dobry Zranchev
    Dobry Zranchev avatar
    1 posts
    Member since:
    Nov 2009

    Posted 03 Dec 2009 Link to this post

    Hi Paul,

    You can use CellFormating event of the grid and there you can format the datetime as you want.

    Kind regards,
    Dobry Zranchev
  3. Martin Vasilev
    Martin Vasilev avatar
    1061 posts

    Posted 04 Dec 2009 Link to this post

    Hello Paul,

    Thank you for the question.

    RadGridView does not support implicit UTC to Local DateTime conversion. You have two ways to work-around this:
    - use CellFormatting event to change the cell displaying value as Dobry Zranchev mentioned.
    - if you are using DataTable to specify DateTimeMode for the DateTime column.

    However, we will consider adding such a property in GridViewDateTimeColumn as well for some of the feature releases. I have updated your Telerik points for bringing our attention to this. Do not hesitate to contact me again if you have any other questions.

    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  4. Dan
    Dan avatar
    5 posts
    Member since:
    Nov 2012

    Posted 20 Mar 2014 in reply to Martin Vasilev Link to this post

    I am doing this with the cellformatting method and the issue I have is the cellformatting is called everytime a click in the grid happens so it constantly subtracts the time offset from the current value, in essence it counts down the datetime on every click.

    if((e.CellElement.ColumnInfo.HeaderText == "FallOff Date" ||
    e.CellElement.ColumnInfo.HeaderText == "Created On" ||
    e.CellElement.ColumnInfo.HeaderText == "Completed On"
    ) && e.CellElement.Value != null)
    DateTime utcDate = DateTime.SpecifyKind((DateTime)e.CellElement.Value, DateTimeKind.Utc);
    e.CellElement.Value = utcDate.ToLocalTime().ToString("MM/dd/yyyy hh:mm:ss tt");
  5. Dan
    Dan avatar
    5 posts
    Member since:
    Nov 2012

    Posted 20 Mar 2014 Link to this post

    I have solved this. I was updating the Value which changes the underlying data object. For display only I should use the Text field of the cell.

    e.CellElement.Text = utcDate.ToLocalTime().ToString("MM/dd/yyyy hh:mm:ss tt");
  6. Dess
    Dess avatar
    2107 posts

    Posted 25 Mar 2014 Link to this post

    Hello Dan,

    Thank you for contacting Telerik Support.

    Changing the cell value in the CellFormatting event is not a recommended approach. However, you can customize the CellElement.Text property as you have already found out. Please refer to our Formatting Cells help article for additional information. I think that our GridViewDateTimeColumn help article will be quite useful about formatting the date via the GridViewDateTimeColumn.FormatString.

    I hope this information helps. Should you have further questions, I would be glad to help.

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

Back to Top