More in this section

Forums / Developing with Sitefinity / How to completely reformat Publication_Date?

How to completely reformat Publication_Date?

4 posts, 1 answered
  1. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    28 Oct 2009
    Link to this post
    I've seen steps to reformat the Publication_Date by adding:

    <%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfCtrl" %>  

    And then adding this:

    <sfCtrl:DateTimeLiteral runat="server" ID="Publication_date" DateFormatString="dd-MMMM-yyyy"></sfCtrl:DateTimeLiteral> 

    However, for my purposes, I need the date to appear in the following format on each blog post. In this example, the true date would be October, 11, 2009:

    OCT
    11

    How can I access the Publication_Date properly from the code-behind to be able to accomplish this in the blog's ItemTemplate?

    Thanks!
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    28 Oct 2009
    Link to this post
    Hello Connections Academy Developer,

    Thank you for using our services.

    What you will have to do is to add two more literal controls to the control templates - one for moth, and one for days. Here is what I have done:
    <p class="sf_postDate">
         <telerik:DateTimeLiteral runat="server" ID="Publication_Date" DateFormatString="MMM dd"></telerik:DateTimeLiteral>
         <asp:Literal ID="Month" runat="server"></asp:Literal>
         </br>
         <p><asp:Literal ID="Day" runat="server"></asp:Literal></p>
    </p>

    Then add this C# script to the ~/Sitefinity/ControlTemplates/Blogs/ContentViewitemList.ascx:
    <script type="text/C#" runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            repeater.ItemDataBound+=new RepeaterItemEventHandler(repeater_ItemDataBound);
        }
        void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Telerik.Cms.Web.UI.DateTimeLiteral dateLiteral=(Telerik.Cms.Web.UI.DateTimeLiteral)e.Item.FindControl("Publication_Date") ;
                string dateString = dateLiteral.Text;
                Literal month = (Literal)e.Item.FindControl("Month");
                month.Text=dateString.Substring(0,3);
                Literal day = (Literal)e.Item.FindControl("Day");
                day.Text=dateString.Substring(4,2);
                dateLiteral.Visible=false;
            }
        }
        </script>

    This would make the original control not visible and parse the date time so it renders as you wish. To make this work for the ~/Sitefinity/ControlTemplates/ContentViewSingleItem.ascx the C# script should be:
    <script type="text/C#" runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            string dateString = this.Publication_Date.Text;
            this.Month.Text = dateString.Substring(0, 3);
            this.Day.Text = dateString.Substring(4, 2);
            this.Publication_Date.Visible = false;
        
        </script>

    Regards,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Answered
  3. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    28 Oct 2009
    Link to this post
    Thanks for the quick response, that worked great!

    Btw, I tried to mark your post as an answer, but the "Mark as Answer" link didn't seem to work.
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    29 Oct 2009
    Link to this post
    Hello Connections Academy Developer,

    I am glad I was able to help. I have also marked the post as answer and we will investigate if there are any issues with the mark as answer button.

    All the best,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
4 posts, 1 answered