示例#1
0
        private static Tuple <DateTime, DateTime> ParseModernLifestyleWebDates(string dateTime)
        {
            if (string.IsNullOrWhiteSpace(dateTime))
            {
                return(null);
            }

            string currentYear = DateTime.Now.Year.ToString();
            string nextYear    = DateTime.Now.AddYears(1).Year.ToString();
            string twoYears    = DateTime.Now.AddYears(2).Year.ToString();

            DateTime startDate = DateTime.MinValue;
            DateTime endDate   = DateTime.MinValue;

            string date = CleanDateString(dateTime).Trim();

            date = ReplaceAbbreviatedMonths(date);

            string startMonth = DateEx.GetMonth(date);
            string endMonth   = DateEx.GetMonth(date);
            string startDay   = "";
            string endDay     = "";
            string year       = currentYear;

            if (dateTime.Contains(nextYear))
            {
                year = nextYear;
            }


            if (dateTime.Contains(twoYears))
            {
                year = twoYears;
            }

            bool found = false;

            string[] dateSplit = date.Split(' ');
            for (int i = 0; i < dateSplit.Length; i++)
            {
                if (dateSplit[i] == "-")
                {
                    startDay = dateSplit[i - 1];
                    endDay   = dateSplit[i + 1].ToSafeString(true);
                    int tmp = -1;
                    if (!int.TryParse(startDay.Trim(), out tmp))
                    {
                        continue;
                    }

                    if (!int.TryParse(endDay.Trim(), out tmp))
                    {
                        endMonth = DateEx.GetMonth(endDay);
                        endDay   = dateSplit[i + 2].ToSafeString(true); //skip ahead two
                    }

                    if (!int.TryParse(endDay.Trim(), out tmp))
                    {
                        Debug.Assert(false, "shouldn't get here");
                        continue;
                    }
                    found = true;
                }
            }

            if (!found)
            {
                //  Fri, Mar 13
                for (int i = 0; i < dateSplit.Length; i++)
                {
                    int day = -1;
                    if (int.TryParse(dateSplit[i].Trim(), out day))
                    {
                        startDay = day.ToString();
                        endDay   = startDay;
                        break;
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(endDay))
            {
                endDay = startDay;
            }

            DateTime.TryParse($"{startMonth} {startDay}, {year}", out startDate);
            DateTime.TryParse($"{endMonth} {endDay}, {year}", out endDate);

            if (endDate == DateTime.MinValue)
            {
                endDate = startDate;
            }

            return(new Tuple <DateTime, DateTime>(startDate, endDate));
        }