Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thor Madsen-Holm 82 posts 212 karma points c-trib
    Apr 11, 2013 @ 21:22
    Thor Madsen-Holm
    0

    Format and display datePicker value in Razor

    Hi everyone, 

    I am an long time xslt user trying to get my figure out how to display a formated date using Razor.

    I am listing some blogposts and want to display a date for each one. The tricky part is that every blog post has a custom property called "overrideDate" that is DatePicker dataType.

    If a blogpost has a value in this property I want to display it, otherwise I want to display the createDate field. 

    I have managed to display the raw date, however I have trouble formatting the date. This is what I have come up with so far: 

    @{
        var blogposts = Model.Descendants("Post").Where("Visible").OrderBy("overrideDate desc, createDate desc");
    }


          @foreach (var post in blogposts)
          {
             

    •             @{
                      var postDate = string.IsNullOrWhiteSpace(post.GetPropertyValue("overrideDate")) ? post.CreateDate : post.GetPropertyValue("overrideDate");

                  }           
                 
                  @postDate


    •     }

    I tried doing:

     @postDate.ToString("dd.MM.yyyy") 

    However that causes an error.

    The weird thing is that it works fine if I do:

    @post.CreateDate.ToString("dd.MM.yyyy") 

    So I am suspecting that it is the custom property thats the root of the error.

    (Note: I am using Umbraco v 6.1.0 beta)

    Does anyone have any idea what I am doing wrong ? Any help would be much appreciated.

    /Thor

  • Dennis Aaen 4434 posts 17891 karma points admin hq c-trib
    Apr 11, 2013 @ 21:37
    Dennis Aaen
    0

    Hi Thor,

    I can see from my traning Umbraco installation that we use on the traning course, We outputtet the date like this,

    Maybe it can inspiring you to find a solution.

    But the code looks like this:

    <ul>
        @foreach (var item in @Model.NewsItems.Where("Visible").OrderBy("CreateDate desc"))
        {
        <li><a href="@item.Url">@item.Name</a>
            <p>
                @item.summary
                <br />
                @item.CreateDate.ToLongDateString(), @item.WriterName
            </p>

        </li>
    }
    </ul>

    So to format the date we are using CreateDate.ToLongDateString()

    /Dennis

  • Thor Madsen-Holm 82 posts 212 karma points c-trib
    Apr 11, 2013 @ 21:40
    Thor Madsen-Holm
    0

    Hi Dennis, 

    Thanks for your reply!

    I tried out your solution, unfortunatly @postDate.ToLongDateString() also returns Error loading MacroEngine script (file: Homepage.cshtml) 

    /Thor  

     

  • Charles Afford 1163 posts 1709 karma points
    Apr 11, 2013 @ 23:27
    Charles Afford
    0

    Thor.  What type is postDate?  Charlie :)

  • Thor Madsen-Holm 82 posts 212 karma points c-trib
    Apr 13, 2013 @ 18:02
    Thor Madsen-Holm
    0

    Hey Carlie, 

    Im not sure what type it is, but not the same as CreateDate I guess.

  • Charles Afford 1163 posts 1709 karma points
    Apr 13, 2013 @ 20:44
    Charles Afford
    0

    Heyyyy :). Sorry thought that was a custom model you were using.  Ummmm you can use the helper method....

     

    @umbraco.library.FormatDateTime

    and the pass in your postDate

     

  • Amir Khan 995 posts 2013 karma points
    Apr 14, 2013 @ 08:57
    Amir Khan
    0

    I think something like this is what you're looking for: @node.postDate.ToString("d MMMM yyyy")

  • Thor Madsen-Holm 82 posts 212 karma points c-trib
    Apr 28, 2013 @ 16:57
    Thor Madsen-Holm
    102

    Hi Charles, 

    I finally got it working using your tip, thx for the input. This is what i ended up with:

     

    @{
      var postDateUnformatted = string.IsNullOrWhiteSpace(post.GetPropertyValue("overrideDate")) ? post.CreateDate : post.GetPropertyValue("overrideDate");
    var postDate = umbraco.library.FormatDateTime(postDateUnformatted.ToString(), "dd MMMM, yyyy");                      
    }

    @postDate

     

    /Thor

  • Charles Afford 1163 posts 1709 karma points
    Apr 28, 2013 @ 18:18
    Charles Afford
    0

    Yep thats the one :).  Glad you got it fixed.

    I think you should be using string instead of var for postDateUnformatted (It must be a string you are getting returned);

    And the postDate should be a DateTime, i dont think should be var as you know you are getting DateTime returned.  Of course nothing to do with the topic and i could be wrong :).  Charlie. 

  • Thor Madsen-Holm 82 posts 212 karma points c-trib
    Apr 28, 2013 @ 18:24
    Thor Madsen-Holm
    0

    Great input Charles! Time for a bit of refactoring.  I'm still new to razor, so any tips are greatly appreciated :-)

  • Charles Afford 1163 posts 1709 karma points
    Apr 28, 2013 @ 18:26
    Charles Afford
    0

    No problem :) Glad it helps.  Any other questions, just ask :)

Please Sign in or register to post replies

Write your reply to:

Draft