Sharepoint 2010 XSLT Date Formatting

The main goal of this article is to outline the snags I hit while trying to configure a SharePoint 2010 Content Query Web Part (CQWP) to display a feed of my recent calendar events. I was presented a very specific layout to use for displaying the latest results from a users calendar, and I knew I would have to jump in and write some custom XSLT for the CQWP to comsume.

Creating a New Style

Hopefully you have access to your site in SharePoint Designer 2010. If so, open your site and navigate to the “/Style Library/XSL Style Sheets” folder, and open “ItemStyle.xsl”. From here, you’ll want to do a few things right off the bat. First, create a new <xsl:template> that your CQWP can comsume. Do this by pasting this code into the bottom of the file BEFORE the <xsl:template name=”HiddenSlots”… line

<xsl:template name="EventDisplay" match="Row[@Style='EventDisplay']" mode="itemstyle">
	<xsl:variable name="dateTime" select="ddwrt:FormatDate(string(@EventDate), 1033, 3)" />
	<xsl:variable name="dateTimeCondensed" select="ddwrt:FormatDate(string(@EventDate), 1033, 2)" />
	<xsl:variable name="date"  select="substring-before(substring-after($dateTime, ', '), ', ')" />
	<xsl:variable name="month" select="substring-before($date, ' ')" />
	<xsl:variable name="day"   select="substring-after($date, ' ')" />
	<xsl:variable name="time"  select="substring-after($dateTimeCondensed, ' ')" />
	<xsl:variable name="SafeLinkUrl">
		<xsl:call-template name="OuterTemplate.GetSafeLink">
		     <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
	<div class="item link-item">
		<div class="description xslCal">
			<div class="left">
				<span class="month"><xsl:value-of select="$month" /></span>
				<span class="day"><xsl:value-of select="$day" /></span>
			<div class="right">
				<span class="title"><xsl:value-of select="@Title" /></span>
		        <span class="location">Location: <xsl:value-of select="@Location" /></span>
				<span class="time">Time: <xsl:value-of select="$time" /></span>


You’ll need to double check that you have

in the top of your xsl file

This will create a new style in your Content Query Web Part’s “Item Style” dropdown.

Add Some CSS

To get this new XSL style to look decent, we’ll add some CSS to make it stand out a bit more. If you already have a custom stylesheet attached to your masterpage, open that up. If not, visit This page for more information on how to do so.

So, with your CSS open, paste in these styles

/*Calendar XSLT Styles*/
.xslCal .left{width:82px; position:relative; left:-21px; float:left;}
.xslCal .right span{display:block;}
.xslCal .month{text-transform:uppercase; background:#71cdf4; color:#fff; padding:3px 0 3px 18px; width:68px; display:block; }
.xslCal .day{font-size:61px; width:86px; display:block; line-height:70px; text-align:right;}
.xslCal .title{color:#0775aa; display:block; padding-top:3px;}

Adding/Configuring a CQWP

Now that we have the proper XSLT and CSS in place, we need to create our data and a Content Query Web Part to show everything. Start by going to Site Actions -> View all site content. Then click the “Create” button in the upper left of the main pane. Select a “Calendar” list type, name it “Main Calendar” and click “Create”. Go into that list, and create a couple sample events on various dates.

Finally, navigate to the page where you want to display your calendar events, and click Site Actions->Edit Page. Then add a Content Query Web Part to the zone you want. Get into the toolpane for that web part by clicking the small black arrow in the title, and selecting “Edit Web Part”. This will open the toolpane on the right side of the screen. Fill out your query by clicking the “Show items from the following list” radio button, and using the “Browse” button, goto the “Main Calendar” list you created earlier. Under the “Content Type” heading, select “List Content Types” from the first dropdown, then “Event” from the second.

Open up the “Presentation” tab and make sure that the Group Style is “Default” and Item Style is “EventDisplay”. Then, to ensure that the correct data goes in the correct slots, setup the “Fields to Display” like shown. If you got it all done correctly, it should look roughly like this:


  1. Cool! Thank you. You can also add the CSS to an Content Editor WebPart.

  2. Benjamin Lloyd says:

    Wonderful post!

    I am also trying to display the description field. I can get it working but its not displaying as html, so i get the tags around everything.

    I have tried searching for a solution but nothing has worked so far…..can you help me?


  3. Mark Leighton says:

    Very neat. But when I open a drop down menu higher up the screen, the blue date block and the day number remain on top of the menu items and obscure them – any idea what I can do about that?

  4. Peter says:

    Did you notice that those styles and transformations are not applied when users are not logged in?

    • sottenad says:

      I’ve never noticed anything like that while using this. Make sure that the xsl and css are checked in and (importantly) have a major version present.

      • Peter says:

        Oh, you are right. I haven’t published it as major version. Thanks!

        Is there any fast and simple way to restrict displayed events (by filtering in CQWP)? I can do it somehow in XSLT but I would like to know if there is some other way to achieve that.

  5. Doug says:

    This is very nice. I do not see my locations coming through, though. Don’t know what I could have done…

  6. Doug says:

    Never mind – I just had to add the group:
    Location [Core Contact and Calendar Columns];

    Thank you for this post!!

  7. […] is a good example what we can do with xslt. Like this:GillaBli först att gilla denna […]

  8. Vinod kumar says:

    Thanks for sharing, just what i wanted. 🙂

  9. Prakash says:

    Is there a way you can link the title to its item?
    Please help me.

    • sottenad says:

      You should be able to change the

       <span class="title"><xsl:value-of select="@Title" /></span> 

      line to something like
      <span class="title"><a href="<xsl:value-of select="$SafeLinkUrl" / rel="nofollow">"><xsl:value-of select="@Title" /></a></span>

      Just off the top of my head that might work.

    • Matt says:

      I’m so close on this, but I can’t get the link to format within the HREF. Outside of it, all works well. Thanks for any help!


      <span><xsl:value-of select="$SafeLinkUrl" /></span>

      Does not work:

      <span class="title"><a href='$SafeLinkUrl' /><xsl:value-of select="@Title" /></a></span>

      • Matt says:

        Actually, I just got it!

        <span class="title"><a href="{$SafeLinkUrl}"><xsl:value-of select="@Title" /></a></span>

  10. Andrei says:

    Hi! Thank you for this post!

    Is there a way to filter data after Start Time? I wan’t to show items when Start Time is greater than Today Date. If I try to apply a filter at Content Query Tool Part Properties (Query), Start Time doesn’t exist in the drop down button.


  11. Candice says:


    Could you please repost the code need for the ItemStyle.xsl?


  12. Paul Cheeseman says:

    Hi there,

    I’ve added the XSL in but it’s killing all my existing CQWPs. The error just says there is a problem displaying the webpart. I’ve already got some custom entries in the itemstyle.xsl file for announcements and link lists.
    Do I need to do any work beforhand to get this to work ?
    This will run against the OOTB sharepoint caldendar lists won’t it ? I’m not a sharepoint dev. person, just a tinkerer.


    • Cliff Bradshaw says:

      Hi Paul,

      This usually indicates that something is wrong with your XSLT code. Look for error indications in your editor and correct them. Otherwise post a snippet of your code.

      • Paul says:

        I’ve just copied the code above into the OOTB ItemStyle.xsl file on a fresh install of Sharepoint and existing CQWP with OTTB formatting break with the “Unable to Display this webpart” error. Do I need add any pre-reqs to the server? I’ve pasted in the CSS and XSLT from above. I don’t expect you to fix it for me, but if you can point out something stupid I’m doing that would be brilliant.

      • Sean says:

        I get the same issue with just copy and pasting the code. I published it as a major version and made sure to past the code in before the HiddenSlots area. Rather odd.

  13. Paul says:

    Fixed it and I love the results. Thank you !!
    I was missing the following from the top of the XSLT file.

  14. Praveen says:

    Thanks a lot for such a great article…. you made my day.
    and thanks paul for adding the missing part.

  15. […] Check out the site here. It’s a simple edit of the ItemStyle.xsl file and adding some CSS: […]

  16. […] Sharepoint 2010 XSLT Date Formatting: 9,459 Views […]

  17. Rahul Sharam says:

    This is fantastic.
    i just copy and paste the above code snippets.

  18. Paul says:

    Hi there,

    I saw this the other day, then when googling for a similar topic, I found your article had been more or less copied out here:

    Just thought you might like to know.

  19. […] the formatting of the Calendar listitems, Steve Ottenad’s post on Sharepoint 2010 XSLT Date Formatting is very handy. Steve’s itemstyle.xsl modifications give a neat layout and CSS for the items, for […]

  20. cferrer says:

    this is cool!!, but now how can I paging if I have more than 4 items?

  21. Paul says:

    Yeah, it does not work. I copied your snippet, pasted exactly where you said, published the item style sheet and it wrecked all my other CQWP’s.

  22. cferrer says:

    this post is nice but with lacking of pagination.

    I could create pagination by extending the CQWP ( also modifying ItemStyle.xsl

    now this CQWP can go from Page 1 to Page x.


  23. enia says:


    I am trying to do this on Sharepoint 2013 and I keep getting an error. Does this solution only work in 2010 or am I missing something?

    • sottenad says:

      Hi Enia, I haven’t revisited this post in quite some time. I don’t know if it will continue to work in SP2013 or not.

      • Jamie says:

        Hey, I have just got this working on O365, looks great

        None of the code needs to change.

        In the web part presentation section –
        Fields to display
        link – blank
        Title – Title;
        EventDate – Start Date;
        Location – Location [Core Contact and Calendar Columns];


  24. Mike says:

    How can I add the link to the event? Also, I can only show 2 event before it shows a scroll bar and I don’t have the height defined in the CQWP, only the width

  25. Mike says:

    how can we make it not show past events and only show future events?

  26. rakesh says:

    Hi Scott,

    How do I sort these events based on date ?
    Currently your solution is showing all events.

Leave a Reply