How to Process DateTime into a Standardized Format When Saved as a Custom Page Type Field?

kentico guy asked on November 20, 2020 20:21

Kentico 12 Portal Engine:

Is there a way that I can transform any datetime to EST in a page type field? I discovered an issue today where even if the user selects PST the page type field always gets saved in EST. IE: if the user creates the event at 9:00AM PST, when I go to the page kentico always uses 9:00AM EST which is the site timezone. Can I use something like a macro in the page type field to process the date into a standardized format? I searched for using macros in page type fields and I didn't find anything.

Does this need to get done with code IE C# or can I do it with a field dependency and a macro?

And what types of methods are available/recommended to standardize time for macros to be consistent across timezones?

Recent Answers

David te Kloese answered on November 20, 2020 21:02

So there are multiple things for displaying times.

First what do you have in the Setting module:

Settings > System

  • Enable time zones
  • Server time zone
  • Site time zone

Is the fact its saved as different timezone the issue? Or is the time displayed wrong for another user?

Because if you start using dates in different formats it can get quite complex for the user. So the default is that the date times will be displayed in format the User selected in their profile, or if nothing was set what is configured as the Site Time zone.

If you want to display a date time differently you could also use a different control and display using a macro and default format: {% String.FormatString("{0:yyyy-MM-dd HH:mm:ss 'GMT' zzz)}", Value) %}

Which would get you something like: 2020-11-20 20:01:14 GMT +00:00

0 votesVote for this answer Mark as a Correct answer

kentico guy answered on November 20, 2020 21:21

Well the issue is that the person who designed this system isn't saving the DateTime in UTC. I've been doing some reading and see that storing dates in UTC is standard practice. I was mostly just wondering if there is a way to create a new page type field that will depend on the localized datetime but it will automatically translate the localized into UTC for another page type field. I could remove the other field but it might cause other issues so I'd rather just keep another copy of the date in UTC.

Do you know if it's possible to generate a hidden page type field that would not be user accessible but it would automatically be generated when another field is filled in? Can I do that with a macro or would I need a code behind file to do it?

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on November 23, 2020 10:42

Are you using the mentioned Time zones feature? If the time zones are set correctly and also the users/editors have the correct time zone set in their account settings, then they should see the time in their time zone.
For the hidden field - yes, you can use depending fields and then use a macro which will read the value from another field.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.