Copied to clipboard

Flag this post as spam?

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


  • Ash 8 posts 28 karma points
    Apr 07, 2011 @ 16:36
    Ash
    0

    how do I format datetime in razor?

    I've searched and I've tried many combinations to no avail. The p#date displays

    4/7/2011 12:00:00 AM 

    using my code below :

     

    <ul>
       @{
    foreach
     (var article in @month.Descendants("NewsArticle").Where("Level == 5"))    {        <li>            <h4>@article.ArticleTitle</h4>           <p id="date">
    @{
               string date = article.ArticleDate.ToString();              library.FormatDateTime(@date, "dd/MM/yyyy");
    }
    </p>             <p>@article.ArticleIntro</p>             <a href="library.NiceUrl(@article.Id)">Read more...</a>          </li>        }
    }
    </ul>

    I would like to make it display just the dd/MM/yyyy format.

     

    Cheers

     



     

  • Daniel Bardi 927 posts 2562 karma points
    Apr 07, 2011 @ 17:59
    Daniel Bardi
    5

    Use this:

    <p id="date">@article.ArticleDate.ToString("dd/MM/yyyy")</p>
  • Ash 8 posts 28 karma points
    Apr 07, 2011 @ 20:25
    Ash
    0

    Nice one. Thanks!

  • Daniel Bardi 927 posts 2562 karma points
    Apr 07, 2011 @ 20:48
    Daniel Bardi
    0

    Your welcome... flag as solution to help others.

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Apr 08, 2011 @ 09:51
    Sebastiaan Janssen
    0

    Don't forget: you can use anything that is in the C# language already. Basically, Razor is C# mixed with HTML and some @ signs.. :)

  • Ash 8 posts 28 karma points
    Apr 08, 2011 @ 10:13
    Ash
    0

    Thanks again, yes, I'm just starting in this, so appreciate the support.

  • Anders Stentebjerg 58 posts 234 karma points
    Nov 25, 2011 @ 16:36
    Anders Stentebjerg
    0

    Hi,

    I got this I want to format with Razor - how to?

    @{
            //For each item node we can then ouput what we want
            foreach (XmlNode node in rssItems)
            {
             
             
             
                <li>
                    <div class="date">@node["pubDate"].InnerText</div>
                    <a href="@node["link"].InnerText">@node["title"].InnerText<span>></span></a>
                </li>
            }
        }
  • Toni Becker 146 posts 425 karma points
    Nov 25, 2011 @ 16:53
    Toni Becker
    0

    hmmmm i think with dot notation it should work. Have to look like this:

    @node.link or @node.title

    or test out

    @node.GetProperty("link").value  or @node.GetProperty("title").value

    just test it out. Some of this should do the trick

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 25, 2011 @ 17:20
    Michael Latouche
    0

    Hi Anders,

    You should use @node.Link, @node.Title etc. The first letter should always be capitalized, unless you want recursive, then you need to prefix with "_"

    You can have a look at the the razor "cheat sheet" for more info : http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    Cheers,

    Michael.

  • Anders Stentebjerg 58 posts 234 karma points
    Nov 28, 2011 @ 10:21
    Anders Stentebjerg
    0

    Thanks for the replies.

    I get the right data out, but I need to format it corret.

    I would like to do something like this

    @node["pubDate"].InnerText.ToString("dd.MM.yyyy")
  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 28, 2011 @ 10:31
    Michael Latouche
    0

    Hi Anders,

    I'm not 100% sure, but one of the following should work:

    @node["pubDate"].Value.ToString("dd.MM.yyyy")

    or

    @node.PubDate.ToString("dd.MM.yyyy")

    Cheers,

    Michael.

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Nov 28, 2011 @ 10:32
    Warren Buckley
    1

    I was helping Anders out on Friday and helped him write a Razor snippet that outputs items from a remote XML source such as an RSS feed, so it's more custom .NET Razor code rather than Umbraco related.
    The RSS item has an XML Node called PubDate that uses a date format such as Thu, 17 Nov 2011 21:37:00 +0100

    Anders to convert this into your own custom date time format you can use the following, which I found from this stackOverflow post
    http://stackoverflow.com/questions/266448/format-rss-pubdate-as-net-datetime

    string orig ="Wed, 29 Oct 2008 14:14:48 +0000";
    string newstring =String.Format("{0:MM/dd/yyyy hh:mm tt}",DateTime.Parse(orig.Remove(orig.IndexOf(" +"))));

    I hope this helps Anders.
    Warren 

     

  • Anders Stentebjerg 58 posts 234 karma points
    Nov 28, 2011 @ 12:30
    Anders Stentebjerg
    0

    Hey Warren,

    Sorry but I still don't get it.

    I think I'll go back to XSLT for now - i got a deadline on the site :(

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Nov 28, 2011 @ 12:39
    Warren Buckley
    1

    Drop me a mail or hit me up on Skype Anders if you want and i'll give you a hand.

    Warren

  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 09:59
    Anthony Candaele
    0

    Hi, is there also a way to format a date in Razor like this:

    "Wednesday 2 March 2012"

    thanks for your help,

    Anthony

  • Daniel Bardi 927 posts 2562 karma points
    May 08, 2012 @ 14:56
    Daniel Bardi
    1

    date.ToString("dddd d MMMM yyyy");

  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 15:00
    Anthony Candaele
    0

    @Daniel Bardi , works! Thanks a lot Daniel

    greetings,

    Anthony

  • Craig100 1136 posts 2523 karma points c-trib
    Jan 09, 2013 @ 03:12
    Craig100
    0

    Hi,

    Any idea why this fails to load the razor script in Umbraco 4.7.0? I just want to output the current year in the copyright statement:-

                <section id="copyright">
                    Copyright © 
    <umbraco:Macro runat="server" language="cshtml">
    @date.ToString("yyyy");
    </umbraco:Macro>
     XXX Projects Ltd. All rights reserved.
                <section>

    Thanks

    Craig

  • Craig100 1136 posts 2523 karma points c-trib
    Jan 09, 2013 @ 09:56
    Craig100
    0

    The answer:

    @DateTime.Now.ToString("yyyy")

    Craig

  • cronynaval 2 posts 51 karma points
    Nov 20, 2014 @ 07:41
    cronynaval
    0

    String to Datetime...

    string iDate = "05/05/2005";
    DateTime oDate = Convert.ToDateTime(iDate);

    DateTime.Parse()

    string iDate = "2005-05-05";
    DateTime oDate = DateTime.Parse(iDate);
    
    string iString = "2005-05-05 22:12 PM";
    DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt",null);
        
    More about ...String to Datetime...
    http://net-informations.com/q/faq/stringdate.html
    Crony
     
     

     

  • Tom Cowling 144 posts 343 karma points
    Mar 11, 2015 @ 15:38
    Tom Cowling
    0

    Going back to Anders' problem, I'm using ver similar code to grab a load of stuff from an external rss feed and then render on my site. Everything works fine, but the date is not in a nice format (standard RSS output).

    I would like to get the solution Warren provided to work, but I'm not sure how to assign my date to the newstring variable that is highlighted in the example.

    @foreach (DataRow entry in entries.Tables["item"].Rows)
          {
           
            if (itemCounter < maxItems)
            {

    So here I would like to switch out the literal string from the example and replace with my @entry["pubDate"] value.

            string orig = "Wed, 29 Oct 2008 14:14:48 +0000";

    Psuedo code would be something like

            string orig = @entry["pubDate"];

    string orig = "Wed, 29 Oct 2008 14:14:48 +0000";        
    string newstring = String.Format("{0:MM/dd/yyyy hh:mm tt}",DateTime.Parse(orig.Remove(orig.IndexOf(" +"))));
           
             <div style="border-bottom: 1px dashed #ccc; display: block; margin: 0px 0px 30px 0px;">
          <h2 style="margin-bottom: 10px;">@entry["title"]</h2>
               <p><em class="itemdate">@entry["pubDate"]</em></p>
               <p>@Library.Truncate(Library.StripHtml((string) entry["description"]), 250)</p>
               <p style="text-align: right;"><a href="@entry["link"]">Read More</a></p>
              </div>
              itemCounter++;
            }
            else
            {
              break;
            }
          }

    Then I could use the newstring value instead of entry["pubDate"].. Any help with syntax would be much appreciated. Razor hurts my brain..

     

    Thanks,

     

    Tom

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 11, 2015 @ 15:47
    Dennis Aaen
    0

    Hi Tom,

    Have you tried something like this, perhaps it works.

    <p><em class="itemdate">@entry["pubDate"].ToString("dd MM yyyy")</em></p>

    I haven't tested it myself, but hope this helps,

    /Dennis

  • Tom Cowling 144 posts 343 karma points
    Mar 11, 2015 @ 15:49
    Tom Cowling
    0

    Tried that and variations on that, but no success.

  • Tom Cowling 144 posts 343 karma points
    Mar 11, 2015 @ 17:02
    Tom Cowling
    0

    Got it!

            string myDate = (string)entry["pubDate"];
            string newDate = String.Format("{0:ddd, dd MMMM yyyy}",DateTime.Parse(myDate.Remove(myDate.IndexOf(" +"))));

    The field needed to be converted into a string for the variable to accept it.

     

    Reference:
    http://stackoverflow.com/questions/5168635/convert-datarow-to-int

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies