Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 503 posts 967 karma points c-trib
    Dec 16, 2015 @ 20:13
    Murray Roke
    0

    Storage Format issues

    Hi, I just backed up my DB and restored it to another pc with a different date format, now I get a format exception. (I'm doing this to get the range from the IContent as at this point it may not be published)

    private DateTime GetEventEndDate(IContent node)
    {
        var raw = node.GetValue("dateRange").ToString();
        if (raw.Contains(",") == false)
        {
            return DateTime.MaxValue;
        }
        return DateTime.Parse(raw.Split(',')[1]);
    }
    

    My suggestion is the date be stored in ISO format then it should always parse and be portable.

    Question 2: is there a better way to get the data in an IContent since this doesn't work, but gives a NullReferenceException:

    return ((IContent)node).GetValue<DateRange>("dateRange").EndDate;
    
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Dec 17, 2015 @ 11:25
    Dan Diplo
    100

    Hi Murray,

    I do store the date in a standard ISO format - it uses ISO 8601 which is YYYY-MM-DDTHH:mm:ss. However, it's really just stored as a CSV string as there are two parts (the start and end). I'm not exactly sure how Umbraco stores these in the database, but I wouldn't have thought it would store them as actual dates. I'm not sure it's anything I'm doing.

    I never really envisioned it being used with IContent. But maybe the best way would be to write an extension method? The code for parsing the dates I use is something like this:

    if (!String.IsNullOrEmpty(sourceString))
    {
        DateRange dr = new DateRange();
    
        var dates = sourceString.Split(',');
    
        if (dates.Length == 2)
        {
            DateTime date;
    
            if (DateTime.TryParseExact(dates[0].Trim(), "yyyy-MM-ddTHH:mm:ss", 
                CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
            {
                dr.StartDate = date;
            }
    
            if (DateTime.TryParseExact(dates[1].Trim(), "yyyy-MM-ddTHH:mm:ss", 
                CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
            {
                dr.EndDate = date;
            }
    
            return dr;
        }
    }
    

    So you could use that as a starting point.

  • 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