Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I'm trying to create a very simple list of articles. On my doctype I have a property called "articleDate" with a Date editor set to mandatory.
My partial view looks like this:
@{ var selection = Model.Content.Children.Where(x => x.IsVisible()).ToArray(); } @if (selection.Length > 0) { <ul> @foreach (var item in selection) { <li> <a href="@item.Url">@Umbraco.Field("articleDate", formatAsDate: true) - @item.Name</a> </li> } </ul> }
But for some reason when outputting the list it replaces "articleDate" with the current date?!?... Am I doing something wrong and if yes.... what?
Hi Bent,
You can also do this
@item.GetPropertyValue<DateTime>("articleDate").ToString("dd/MM/yyyy")
Of course you need to change the format in the ToString to your desired format
Dave
You're using @Umbraco.Field which will attempt to get the data from the current page node not the children so you want to use the item!
You want something like:
@item.GetPropertyValue<DateTime>("articleDate")
And you can control the format (I think you're in the US?) via something
like:
@(item<DateTime>("articleDate").ToString("MMM yyyy ddd d HH:mm"))
Once your razor to format starts looking this complicated it's probably worth breaking it out a bit. So something like:
@{ var selection = Model.Content.Children.Where(x => x.IsVisible()).ToArray(); } @if (selection.Length > 0) { <ul> @foreach (var item in selection) { var formatedArticleDate = item.GetPropertyValue<DateTime>("articleDate").ToString("MMM yyyy ddd d HH:mm"); <li> <a href="@item.Url">@formatedArticleDate - @item.Name</a> </li> } </ul> }
HTH
Steve
EDIT: Dave is much faster at typing than me
Probably because my answer is way shorter and not explaining why his approach didn't work :-)
Of course... And very nice answer :)
Cheers!
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Problem with showing date from date picker
I'm trying to create a very simple list of articles. On my doctype I have a property called "articleDate" with a Date editor set to mandatory.
My partial view looks like this:
But for some reason when outputting the list it replaces "articleDate" with the current date?!?... Am I doing something wrong and if yes.... what?
Hi Bent,
You can also do this
Of course you need to change the format in the ToString to your desired format
Dave
Hi Bent,
You're using @Umbraco.Field which will attempt to get the data from the current page node not the children so you want to use the item!
You want something like:
And you can control the format (I think you're in the US?) via something
like:
Once your razor to format starts looking this complicated it's probably worth breaking it out a bit. So something like:
HTH
Steve
EDIT: Dave is much faster at typing than me
Probably because my answer is way shorter and not explaining why his approach didn't work :-)
Dave
Of course... And very nice answer :)
Cheers!
is working on a reply...