Custom News User Control Date Time Zone

Posted by Community Admin on 03-Aug-2018 21:31

Custom News User Control Date Time Zone

All Replies

Posted by Community Admin on 09-May-2011 00:00

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!

Posted by Community Admin on 10-May-2011 00:00

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.

Posted by Community Admin on 16-May-2011 00:00

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

This thread is closed