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.
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.
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:
@foreach (var post in blogposts)
{
@{
var postDate = string.IsNullOrWhiteSpace(post.GetPropertyValue("overrideDate")) ? post.CreateDate : post.GetPropertyValue("overrideDate");
}
@postDate
}
I tried doing:
However that causes an error.
The weird thing is that it works fine if I do:
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
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:
So to format the date we are using CreateDate.ToLongDateString()
/Dennis
Hi Dennis,
Thanks for your reply!
I tried out your solution, unfortunatly @postDate.ToLongDateString() also returns Error loading MacroEngine script (file: Homepage.cshtml)
/Thor
Thor. What type is postDate? Charlie :)
Hey Carlie,
Im not sure what type it is, but not the same as CreateDate I guess.
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
I think something like this is what you're looking for: @node.postDate.ToString("d MMMM yyyy")
Hi Charles,
I finally got it working using your tip, thx for the input. This is what i ended up with:
/Thor
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.
Great input Charles! Time for a bit of refactoring. I'm still new to razor, so any tips are greatly appreciated :-)
No problem :) Glad it helps. Any other questions, just ask :)
is working on a reply...