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
Hi,
I got this code working almost ok (App_Code\dateRouting.cs):
using Umbraco.Core.Models; using Umbraco.Core.Strings; namespace Umbraco8.Routing { public class DatePageUrlSegmentProvider : IUrlSegmentProvider { readonly IUrlSegmentProvider _provider = new DefaultUrlSegmentProvider(); public string GetUrlSegment(IContentBase content, string culture = null) { if (!(content.ContentType.Alias == "siteEvent" || content.ContentType.Alias == "siteNewsPage")) return null; var segment = _provider.GetUrlSegment(content); string[] dateArray = content.GetValue<string>("date").Split('/'); return string.Format("{1}-{0}", segment, dateArray[2].Substring(0, 4) + "-" + dateArray[1] + "-" + dateArray[0]); } } }
It routes news and events so they get a more friendly url with their date in it.
But I found out that when changing my backend language from English to Danish then I get a out of bound index error on the dateArray
I thought not that the date format in the backend changed with the language but it does apparently.
Can someone give me a better way (more generic) to get year (yyyy), month (mm) and day (dd) from the date property?
Hi Martin.
What type of property are you using for the date? A date picker? Or just a string?
If you are using a date picker or if the format for the date is correct, you should be able to parse the string to a DateTime object: https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparse?view=net-5.0
TL;DR:
var couldParseDate = DateTime.TryParse("2021/02/23", out var date);
Hope this helps a little bit.
Regards Malthe
Thanks Malthe. I was lead in another direction in the Umbraco Web Developers forum.
Instead of
string[] dateArray = content.GetValue<string>("date").Split('/'); return string.Format("{1}-{0}", segment, dateArray[2].Substring(0, 4) + "-" + dateArray[1] + "-" + dateArray[0]);
I use
var date = content.GetValue<DateTime>("date"); string yearStr = date.Year.ToString(); string monthStr = date.Month.ToString("00"); string dayStr = date.Day.ToString("00"); return string.Format("{1}-{0}", segment, yearStr + "-" + monthStr + "-" + dayStr);
And that did the job. :)
Great! Remember a null check for the value, otherwise you could end up with an unwanted result.
Good point. In this solution the "date" property is required in the backend, but thats not very generic. A generic solution would be setting date to create time of the node if null. :)
Would it not be easier just to make the DateTime a string?
var date = content.GetValue<DateTime>("date"); return date.ToString("segment-yyyy-MM-dd");
Of course! You are so right. :)
Final code:
var segment = _provider.GetUrlSegment(content); var date = content.GetValue<DateTime>("date"); return date.ToString("yyyy-MM-dd-") + segment;
is working on a reply...
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.
Continue discussion
Get year (yyyy), month (mm) and day (dd) from the date property
Hi,
I got this code working almost ok (App_Code\dateRouting.cs):
It routes news and events so they get a more friendly url with their date in it.
But I found out that when changing my backend language from English to Danish then I get a out of bound index error on the dateArray
I thought not that the date format in the backend changed with the language but it does apparently.
Can someone give me a better way (more generic) to get year (yyyy), month (mm) and day (dd) from the date property?
Hi Martin.
What type of property are you using for the date? A date picker? Or just a string?
If you are using a date picker or if the format for the date is correct, you should be able to parse the string to a DateTime object: https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparse?view=net-5.0
TL;DR:
Hope this helps a little bit.
Regards Malthe
Thanks Malthe. I was lead in another direction in the Umbraco Web Developers forum.
Instead of
I use
And that did the job. :)
Great! Remember a null check for the value, otherwise you could end up with an unwanted result.
Good point. In this solution the "date" property is required in the backend, but thats not very generic. A generic solution would be setting date to create time of the node if null. :)
Would it not be easier just to make the DateTime a string?
Of course! You are so right. :)
Final code:
is working on a reply...
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.