Displaying correct time in your code

You can use the following methods in the code of ASCX transformations to display the correct time according to time zone settings.

<%# GetDateTime(DateTime.Now) %>

Returns date-time value according to web part time zone settings.

<%# GetSiteDateTime(DateTime.Now) %>

Returns date-time according to site time zone settings.

<%# GetUserDateTime(DateTime.Now) %>

Returns date-time according to the current user’s time zone settings.

<%# GetCustomDateTime(DateTime.Now, “GreenwichMeanTime”) %>

Returns date-time according to the time zone given in the second parameter.


The following example demonstrates how to use the User control web part to display the current date and time on your site, according to the time zone settings of the web part.

  1. Open your web project in Visual Studio

  2. Add a folder named CMSGlobalFiles (if it doesn’t already exist).

  3. Create a new Web User Control in the folder named datetime.ascx.

  4. Add a Label control onto the user control’s form.

  5. Edit the control’s code behind and add the following code to the Page_Load method:

     Label1.Text = CMS.Globalization.Web.UI.TimeZoneUIMethods.ConvertDateTime(DateTime.Now, this).ToString();
  6. Log in to the Kentico administration interface.

  7. Open the Pages applications and edit a page on the Design tab.

  8. Add the User control web part onto the page.

  9. Set the following properties of the web part:

    • User control virtual path: ~/cmsglobalfiles/datetime.ascx
    • Time zone: Custom
    • Custom time zone: any time zone of your choice
  10. Click OK.

If you switch to the live site, you can see the web part displaying the current date and time in the selected time zone. You can also try changing the value of the Custom time zone property and verify that the time displayed on the live site changes accordingly.