Converting From SPSystemTime From A Reference Date

If you are working with SPSystemTime struct you will have run into this. Here is a utility method that makes it really easy to get around:

[csharp]

        public static DateTime ConvertSPSystemTimeToDateTime(SPSystemTime st, DateTime refDate)
        {
            if (st.Month == 0)
            {
                return DateTime.MinValue;
            }
            DateTime time = new DateTime(refDate.Year, st.Month, 1);
            DayOfWeek week = (DayOfWeek) Enum.ToObject(typeof (DayOfWeek), st.DayOfWeek);
            if (time.DayOfWeek > week)
            {
                time = time.AddDays((7 – (time.DayOfWeek – week)));
            }
            else if (time.DayOfWeek < week)
            {
                time = time.AddDays((week – time.DayOfWeek));
            }
            time = time.AddDays((7*(st.Day – 1)));
            while (time.Month > st.Month)
            {
                time = time.AddDays(-7.0);
            }
            return time.Add(new TimeSpan(st.Hour, st.Minute, 0));
        }

[/csharp]

Share

Leave a Reply

Your email address will not be published. Required fields are marked *