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:
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;
}
}
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)
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:
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:
So you could use that as a starting point.
is working on a reply...