Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 231 posts 901 karma points c-trib
    Feb 23, 2021 @ 16:00
    Martin Rud
    0

    Get year (yyyy), month (mm) and day (dd) from the date property

    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?

  • Malthe Petersen 68 posts 383 karma points c-trib
    Feb 23, 2021 @ 17:20
    Malthe Petersen
    0

    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

  • Martin Rud 231 posts 901 karma points c-trib
    Feb 23, 2021 @ 18:59
    Martin Rud
    101

    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. :)

  • Malthe Petersen 68 posts 383 karma points c-trib
    Feb 23, 2021 @ 19:10
    Malthe Petersen
    0

    Great! Remember a null check for the value, otherwise you could end up with an unwanted result.

  • Martin Rud 231 posts 901 karma points c-trib
    Feb 23, 2021 @ 19:24
    Martin Rud
    0

    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. :)

  • Bo Jacobsen 590 posts 2366 karma points
    Feb 23, 2021 @ 21:35
    Bo Jacobsen
    0

    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");
    
  • Martin Rud 231 posts 901 karma points c-trib
    Feb 23, 2021 @ 23:13
    Martin Rud
    0

    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;
    
Please Sign in or register to post replies

Write your reply to:

Draft