Try our conversational search powered by Generative AI!

List of DateTime Property and DateTimeKind

Vote:
 

I am trying to update page property programmatically.

The type of the property is IList<DateTime>.

The source list of dates contains only UTC dates  (DateTimeKind.Utc)

After saving changes and re-reading page object it turns out that all dates are converted to Local time

The question is: what a heck?

Code demo is below.

List<DateTime> newDays = GetDays(); // all UTC dates, 

HomePage page = _contentLoader.Get<HomePage>(pageRefHere);
var clone = page.CreateWritableClone() as HomePage;
clone.SpecialDateTimes = newDays; // SpecialDateTimes is still in utc here
_contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);

page = _contentLoader.Get<HomePage>(pageRefHere); // re-read page 

var days = page.SpecialDateTimes; // all dates are local now
#282312
Edited, Jun 23, 2022 8:58
Vote:
 

It's usual for any dates shown in the UI to be converted in to the local users time based on their locale settings so if you're seeing this in the CMS backend that's quite normal.

Dates in the database should be saved as UTC then the UI takes care of localization for users.

#282313
Jun 23, 2022 9:11
Vote:
 

> It's usual for any dates shown in the UI

I can't argue with that, but I am getting the page on backend part to do calculations and I guess it is more convinient to work with universal time

Convertion to local time should be on FE part, just as you said.

#282314
Edited, Jun 23, 2022 9:29
Vote:
 

One thing missing in your code is the actual Saving / Publishing code that should be after clone.SpecialDateTimes = newDays;

Just in case you're seeing weird behavior as you've not actually saved it you should add that in, where below _repository is IContentRepository

_repository.Save(page, SaveAction.Publish);
#282315
Jun 23, 2022 9:33
klappo - Jun 23, 2022 10:00
you are right, but it doesn't change much in the topic
Scott Reed - Jun 23, 2022 10:02
The APIs I believe convert to UTC based on the server setup. So there's a chance that not doing saving or publish hadn't gone through those processes ergo I thought it would be relevent. It might be if your local server time isn't UTC it might be attempting to convert
klappo - Jun 23, 2022 10:07
> it might be attempting to convert
and again, I don't see a good reason why it does so on BE part. I could understand if that happened on FE side of even may be in ContentDeliveryApi
Vote:
 
List<DateTime> newDays = GetDays(); // all UTC dates, 

HomePage page = _contentLoader.Get<HomePage>(pageRefHere);
var clone = page.CreateWritableClone() as HomePage;
clone.SpecialDateTimes = newDays; // SpecialDateTimes is still in utc here
 _contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);

page = _contentLoader.Get<HomePage>(pageRefHere); // re-read page 

var days = page.SpecialDateTimes; // all dates are local now

updated code example

#282318
Jun 23, 2022 10:01
Vote:
 

no solution

#283986
Jul 20, 2022 7:42
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.