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. |
Example
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.
Open your web project in Visual Studio
Add a folder named CMSGlobalFiles (if it doesn’t already exist).
Create a new Web Use Control in the folder named datetime.ascx.
Add a Label control onto the user control’s form.
Edit the control’s code behind and add the following code to the Page_Load method:
Label1.Text = CMS.Globalization.TimeZoneMethods.ConvertDateTime(DateTime.Now, this).ToString();
Log in to the Kentico administration interface.
Open the Pages applications and edit a page on the Design tab.
Add the User control web part onto the page.
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
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.