1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Custom News User Control Date Time Zone

Custom News User Control Date Time Zone

3 posts, 0 answered
  1. Jeremy
    Jeremy avatar
    12 posts
    Registered:
    28 Feb 2007
    09 May 2011
    Link to this post
    I am creating a custom widget that will show news articles based on a couple of custom date fields in the News module. Basically, I want to only show a list of articles where DateTime.Now is between the field FrontPagePublishDate and FrontPageExpireDate fields. I have a repeater with the following code in Page_Load:

    rptNews.DataSource = App.WorkWith()
       .NewsItems()
       .Publihed()
       .Where(nI => nI.GetValue<System.Nullable<DateTime>>("FrontPagePublishDate") <= DateTime.Now)
       .Where(nI => nI.GetValue<System.Nullable<DateTime>>("FrontPageExpireDate") > DateTime.Now)
       .Get()
       .ToList();
     
    rptNews.DataBind();

    All is well except for the fact that it is comparing with the wrong time zone. We are in the Eastern Time zone (EDT) and the fields contain UTC times I guess because they are 5 hours off. How can I fix this? I tried using this:

    rptNews.DataSource = App.WorkWith()
       .NewsItems()
       .Publihed()
       .Where(nI => SystemExtensions.ToLocal(nI.GetValue<System.Nullable<DateTime>>("FrontPagePublishDate")) <= DateTime.Now)
       .Where(nI => SystemExtensions.ToLocal(nI.GetValue<System.Nullable<DateTime>>("FrontPageExpireDate")) >DateTime.Now)
       .Get()
       .ToList();
      
    rptNews.DataBind();

    But ended up getting this error at runtime:

        Execution of 'Telerik.Sitefinity.SystemExtensions:ToLocal(DateTime)' on the database server side currently not implemented.


    Please help. Thanks!
  2. Jeremy
    Jeremy avatar
    12 posts
    Registered:
    28 Feb 2007
    10 May 2011
    Link to this post
    I was able to change the code to this instead:

    rptNews.DataSource = App.WorkWith()
       .NewsItems()
       .Publihed()
       .Where(nI => (DateTime)nI.GetValue<System.Nullable<DateTime>>("FrontPagePublishDate") <= DateTime.Now.ToUniversalTime())
       .Where(nI => (DateTime)nI.GetValue<System.Nullable<DateTime>>("FrontPageExpireDate") > DateTime.Now.ToUniversalTime())
       .Get()
       .ToList();
     
    rptNews.DataBind();

    But I am still curious about the .ToLocal method.
  3. Nikolay Datchev
    Nikolay Datchev avatar
    87 posts
    Registered:
    01 Nov 2016
    16 May 2011
    Link to this post
    Hi Jeremy,

    The ToLocal is an extension method that is offsetting some date according to SystemConfig.UITimeZoneSettings configuration settings.  This is intended to also allow for different server timezones depending on the user profile in the future.

    When you query data in the database you should always assume and ensure the dates are in UTC timezone, so you don't need this  method - this method is intended only when presenting UTC dates in the frontend UI. The error that you receive is because Open Access LINQ queries parser  - cannot interpret this method - since it is Sitefinity specific logic.

    Best wishes,
    Nikolay Datchev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
3 posts, 0 answered