Custom News User Control Date Time Zone
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();
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();
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();
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