GetDateTime(DateTime.Now) returns page publish time instead of current time

Mike Bilz asked on June 11, 2019 20:19

Hello Kentico Team,

I am attempting to include the time a page was loaded within an editable text area on a page, however the macro appears to resolve to when the page was last saved, rather than to the time the page was loaded by the user. Even if the page is reloaded, the macro always returns the same value. The value doesn't change until the page is re-saved.

Here is the Macro I am using:

{% String.FormatString("{0:ss}", GetDateTime(DateTime.Now)) #%}

Is there an additional command or something that I need to include so that DateTime.Now is assessed on page load? Does this macro need to be written inside of a special webpart or something instead of inline within a text area?

Thanks in advance.

-mike

Correct Answer

Dmitry Bastron answered on June 12, 2019 12:15

Hi Mike,

Do you have output cache enabled for this page? Output cache caches the HTML output with all the macroses resolved and it may be the reason your macro is not calculated on every page load.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 12, 2019 09:46 (last edited on December 10, 2019 02:31)

Have you simply tried this macro:

{%DateTime.Now.TimeOfDay.ToString()|(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on June 12, 2019 09:50

Where are you using the macro? I tried it on the Page tab in an editable region as well as on a Form tab field and rendering through a transformation and it is working fine. Also, if you want to get current date time, use just DateTime.Now, you do not need to use the additional macro method to get date from a date field.

0 votesVote for this answer Mark as a Correct answer

Mike Bilz answered on June 13, 2019 18:17

Thanks Dmitry,

Yep, I had forgotten about the Partial Cache settings for the text field, which are great for performance but make chaos for Macros.

I was able to solve my issue by creating an ad-hoc template and removing the Partial Cache from that field, now the content is loaded fresh each time it is viewed and the macro is fired on page load as expected.

Is there another way to bypass the Partial Cache? maybe from within the macro? The ad-hoc solution works for this instance, but it isn't really scalable.

Thanks again everyone.

-mike

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on June 13, 2019 19:04

No, there is no other easy way of bypassing output cache. Only by not enabling it. =)

0 votesVote for this answer Mark as a Correct answer

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