Convert Date time output to TimeStamp for macros in Javascript webpart

Jose Zamorano asked on August 12, 2016 09:37

Hi There,

I searched the Kentico documentation for Macros and the forums with no luck. I am embedding javascript code in a portal Masterpage of our current project for a third party vendor that requires some logged-in user information and I am feeding the data to javascript via macros.

From the code inserted in the in-line script of the javascript web part the output is as follows:

Code: user : {% CurrentUser.UserActivationDate #%}

Output:
user : 8/1/2016 1:31:21 PM

However I need as output the number of seconds since 1/1/1970 to be sent to this third party vendor. How can I convert the resulting value to Unix timestamp using macros?

Thank you very much for your help!

Correct Answer

Kristian Bortnik answered on August 12, 2016 15:46

To get the number of seconds from 1/1/1970 to your date (CurrentUser.UserActivationDate):

{%Math.Floor((CurrentUser.UserActivationDate - Convert.ToDateTime("1/1/1970")).TotalSeconds)|(identity)GlobalAdministrator%}

You will need to be careful with cultures, as they could skew the output of the macro.

2 votesVote for this answer Unmark Correct answer

Recent Answers


Jan Hermann answered on August 12, 2016 09:59 (last edited on December 10, 2019 02:30)

You can either create custom macro method to calculate it or you can use following approach (not so elegant):

{%
CurrentUser.UserActivationDate.Second + (CurrentUser.UserActivationDate.Minute * 60) + (CurrentUser.UserActivationDate.Hour * 3600) + (CurrentUser.UserActivationDate.DayOfYear * 86400) + (secondsTill2016)
|(identity)GlobalAdministrator%}
3 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on August 12, 2016 10:49

I guess you should create your custom macro method for DateTime object: Registering custom macro methods

0 votesVote for this answer Mark as a Correct answer

Richard Sustek answered on August 12, 2016 12:09 (last edited on December 10, 2019 02:30)

Edit: Oops, I read that wrong. The previous answer from Jan is correct one.

No need for custom macros. All you need to do is:

{% GetDateTime(CurrentDateTime, "MM/dd/yyyy")%}

or in your case:

{% GetDateTime(CurrentUser.UserActivationDate, "MM/dd/yyyy")|(identity)GlobalAdministrator%}

The macro takes standard C# formatting string from msdn

Richard

2 votesVote for this answer Mark as a Correct answer

Jose Zamorano answered on August 15, 2016 02:33 (last edited on August 15, 2016 03:08)

Thank you Kristian Bortnik! I marked your answer as the correct answer. Have a great day!

0 votesVote for this answer Mark as a Correct answer

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