Skip to main content

How To: Add Time/Date to Layouts or Master Page

If you ever needed to add in the local time and a date for the logged in user to your custom page layout or master page below are some ways that you can achieve this.

Option #1
The first method uses a SharePoint Portal Web Control to display the logged in users current time. This control is normally used on the profile pages to show current time for the profile that you are viewing. One benefit that you get from this approach is that if you have users that view the site from multiple locations around the world they would see the current time based on the time zone that they have set in their my site Profiles.

To add this web control to your master page do the following:

  1. Add in the following registration to the top of the master page:
    <%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  2. Add in the the following code right below the welcome control “IdWelcome” DIV in your master page.
    <div class="currenttime">
        <SPSWC:ProfilePropertyLoader runat="server"/>
        <SPSWC:LocalTimeControl runat="server" />
    </div>
  3. Then add in some quick styles to your custom style sheet so that the time is set to white.
    .currenttime{
        color: #FFF;
        text-align: right;
        font-family: Arial, sans-serif;
        font-weight: normal;
        font-size: 12px;
    }

Your site should look something like this:
image

Notice the time below the users name. Now if the logged in user changes the time zone in their profile it will automatically update the time on all the pages that use this master page.
image

I am not aware of any web control that displays the date/month/year. If you know of one please reply to this post.

Option #2
The second option is that you could use JavaScript within your page layouts or in your master page to display the current time and date of for the logged in user. The time zone is based on the settings from the computer that is accessing the site.

image

To add this JavaScript to your page layout do the following:

  1. Add in the following code anywhere within your page layout:
    <table cellpadding="0" cellspacing="0">
        <tr>
            <td class="company-homepagetime" valign="top">
                <script type="text/javascript">
                    function getClockTime()
                    {
                        var now    = new Date();
                        var hour   = now.getHours();
                        var minute = now.getMinutes();
                        var second = now.getSeconds();
                        var ap = "<span class='company-homepagetime-ampm'>AM</span>";
                        if (hour   > 11) { ap = "<span class='company-homepagetime-ampm'>PM</span>";}
                        if (hour   > 12) { hour = hour - 12;      }
                        if (hour   == 0) { hour = 12;             }
                        if (minute < 10) { minute = "0" + minute; }
                        if (second < 10) { second = "0" + second; }
                        var timeString = hour +
                            ':' +
                            minute +
                            " " +
                            ap;
                        return timeString;
                    } // function getClockTime()
                        var clockTime = getClockTime();
                        document.write(clockTime);
                </script>
            </td>
        </tr>
        <tr>
            <td class="company-homepagedate" valign="top">
                <script type="text/javascript">
                    var months=new Array(13);
                    months[1]="January";
                    months[2]="Febuary";
                    months[3]="March";
                    months[4]="April";
                    months[5]="May";
                    months[6]="June";
                    months[7]="July";
                    months[8]="August";
                    months[9]="September";
                    months[10]="October";
                    months[11]="November";
                    months[12]="December";
                    var day=new Date();
                    var lmonth=months[day.getMonth() + 1];
                    var date=day.getDate();
                    var year = day.getFullYear();
                    document.write(lmonth + " " + date + ", " + year);
                </script>
            </td>
        </tr>
    </table>
  2. Then Add in the following CSS to give it a little style
    .company-homepagetime{
        color: #000;
        font-family: Arial, sans-serif;
        font-weight: normal;
        font-size: 30px;
        text-align: left;
        padding: 10px 0px 0px 15px;
    }
    .company-homepagetime-ampm{
        color: #000;
        font-family: Arial, sans-serif;
        font-weight: normal;
        font-size: 20px;
        text-align: left;
    }
    .company-homepagedate{
        color: #000;
        font-family: Arial, sans-serif;
        font-size: 14px;
        text-align: left;
        text-transform:uppercase;
        padding: 0px 0px 0px 17px;
    }

The result should look similar to the following:

image

You could add in a content placeholder in your master page above the left navigation, or in another location so that the time can be displayed anywhere you want.

I think the second option that is using JavaScript is a lot more flexible and allows you to customize the display and format of the time and date. For example if you wanted to truncate the months to three characters all you would have to do is simply type in what it should display in the code.
Example:(Change “February” to “Feb” or “November” to “Nov”).

Post a comment if you found this useful or have any other type of solution that worked for you.

Thanks for listening,

Erik Swenson (Author)
My Book: Practical SharePoint 2010 Branding and Customization

Comments

Lawrence Giles said…
Great find, I’m going to have to check this one out. Thanks for sharing.
Very informative….
Anonymous said…
I'm working with option #2

I have added the 1st Javascrpit and the Data and Time are working on the page.

How do I add the CSS portion?

Thanks!

Popular posts from this blog

SharePoint 2010 Base CSS Classes

This will be the first of many SharePoint 2010 posts. I will be focusing on a few of the main CSS classes used for SharePoint 2010 Public Beta. As the product becomes more final there might be some changes to the class names but I will be sure to create a new post if that happens. This will be quite a lengthy but it should be helpful. The default CSS given below are just highlights of the full CSS attributes for that class. I will be using a basic team site as my base for the screenshots. Here is a basic structure of the main areas that I will cover. Ribbon Row Table Row Left Site Actions Navigate Up Edit Tab List Browse Page Table Row Right Give Feedback Welcome Menu Workspace Body Container Title Row Title ...

SharePoint 2010 Content Query for Blog Posts

I hope this post will help many of you feel comfortable with using the Content Query Web Part. In this post I will walk you through the process of creating a content query web part and configuring it to show custom field types. I will also give details on how to use XSLT to stylize and format the data being pulled. I will be using the following scenario as an example. Say that you had a site collection with a top level publishing site. This publishing site would display a the most recent blog posts from all blog sites within its own site collection. To solve this problem we will use a Content Query Web Part and a customized ItemStyle.xsl using XSLT. Please note that the “SharePoint Server Publishing Infrastructure” needs to be enabled at the site collection to display the content query web part. Step 1: Add a Content Query Web Part to Page Navigate to the site that you want the blog posts to show up and click on edit page. Under Editing Tools in the Ribbon, Click on ...

SharePoint 2013 Responsive Table Columns

I have been wanting to write this one for a while now. It is really amazing how UX is really finding is way into everything that we use and interact with. From Custom applications both mobile and on a desktop to document management or large data visualizations. There is always room for better usability and new concepts. SharePoint lists and library functionality really has not changed much for the past 10 years... I remember back in 2003 when I saw the same table/grid based views of documents and list items that exists in SharePoint 2013. But now we can look at them in a whole new way! In this video blog you will see how to create a responsive CSS table so that when the browser size is reduced it will hide specific columns. However hiding data is not always the right thing to do. What if a user needed those columns to filter on or to use for comparison to another document? Well that is where the custom jQuery Column chooser comes in. It allows you to see what columns are displ...