public static object DateDiff(string datepart, object o1, object o2)
        {
            if (string.IsNullOrEmpty(datepart))
            {
                throw new InvalidOperationException("Datepart can not be empty.");
            }

            var dp = DateParts.GetDatePart(datepart);

            if (EDatePart.Unknown == dp)
            {
                throw new InvalidOperationException($"'{datepart}' is not a recognized datediff option.");
            }
            if (IsNull(o1) || IsNull(o2))
            {
                return(DBNull.Value);
            }

            DateTime dt1;

            try
            {
                dt1 = Convert.ToDateTime(o1);
            }
            catch
            {
                throw new InvalidOperationException($"Conversion failed when converting date and/or time from '{o1.ToString()}'.");
            }

            DateTime dt2;

            try
            {
                dt2 = Convert.ToDateTime(o2);
            }
            catch
            {
                throw new InvalidOperationException($"Conversion failed when converting date and/or time from '{o2.ToString()}'.");
            }

            switch (dp)
            {
            case EDatePart.Year:
                return(dt2.Year - dt1.Year);

            case EDatePart.Quarter:
                return((dt2.Year - dt1.Year) * 4 + (dt2.Month - dt1.Month) / 3);

            case EDatePart.Month:
                return((dt2.Year - dt1.Year) * 12 + (dt2.Month - dt1.Month));

            case EDatePart.DayOfYear:
            case EDatePart.Day:
            case EDatePart.WeekDay:
                var days = (dt2.Date - dt1.Date).TotalDays;
                return(Math.Round(days));

            case EDatePart.Week:
                var weeks = (dt2.Date - dt1.Date).TotalDays / 7;
                return(Math.Round(weeks));

            case EDatePart.Hour:
                var hours = (dt2 - dt1).TotalHours;
                return(Math.Round(hours));

            case EDatePart.Minute:
                var minutes = (dt2 - dt1).TotalMinutes;
                return(Math.Round(minutes));

            case EDatePart.Second:
                var seconds = (dt2 - dt1).TotalSeconds;
                return(Math.Round(seconds));

            case EDatePart.MilliSecond:
                return((dt2 - dt1).TotalMilliseconds);

            default:
                throw new InvalidOperationException($"'{datepart}' is not a recognized datediff option.");
            }
        }
        public static object DatePart(string datepart, object o)
        {
            if (string.IsNullOrEmpty(datepart))
            {
                throw new InvalidOperationException("Datepart can not be empty.");
            }

            var dp = DateParts.GetDatePart(datepart);

            if (EDatePart.Unknown == dp)
            {
                throw new InvalidOperationException($"'{datepart}' is not a recognized datepart option.");
            }
            if (IsNull(o))
            {
                return(DBNull.Value);
            }

            DateTime dt;

            try
            {
                dt = Convert.ToDateTime(o);
            }
            catch
            {
                throw new InvalidOperationException($"Conversion failed when converting date and/or time from '{o.ToString()}'.");
            }

            switch (dp)
            {
            case EDatePart.Year:
                return(dt.Year);

            case EDatePart.Quarter:
                return((dt.Month / 3) + 1);

            case EDatePart.Month:
                return(dt.Month);

            case EDatePart.DayOfYear:
                return(dt.DayOfYear);

            case EDatePart.Day:
                return(dt.Day);

            case EDatePart.WeekDay:
                return((int)dt.DayOfWeek + 1);

            case EDatePart.Week:
                return(CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Sunday));

            case EDatePart.Hour:
                return(dt.Hour);

            case EDatePart.Minute:
                return(dt.Minute);

            case EDatePart.Second:
                return(dt.Second);

            case EDatePart.MilliSecond:
                return(dt.Millisecond);

            default:
                throw new InvalidOperationException($"'{datepart}' is not a recognized datepart option.");
            }
        }
        public static object DateAdd(string datepart, int num, object o)
        {
            if (string.IsNullOrEmpty(datepart))
            {
                throw new InvalidOperationException("Datepart can not be empty.");
            }

            var dp = DateParts.GetDatePart(datepart);

            if (EDatePart.Unknown == dp)
            {
                throw new InvalidOperationException($"'{datepart}' is not a recognized dateadd option.");
            }
            if (IsNull(o))
            {
                return(DBNull.Value);
            }

            DateTime dt;

            try
            {
                dt = Convert.ToDateTime(o);
            }
            catch
            {
                throw new InvalidOperationException($"Conversion failed when converting date and/or time from '{o.ToString()}'.");
            }

            switch (dp)
            {
            case EDatePart.Year:
                return(dt.AddYears(num));

            case EDatePart.Quarter:
                return(dt.AddMonths(num * 3));

            case EDatePart.Month:
                return(dt.AddMonths(num));

            case EDatePart.DayOfYear:
            case EDatePart.Day:
            case EDatePart.WeekDay:
                return(dt.AddDays(num));

            case EDatePart.Week:
                return(dt.AddDays(num * 7));

            case EDatePart.Hour:
                return(dt.AddHours(num));

            case EDatePart.Minute:
                return(dt.AddMinutes(num));

            case EDatePart.Second:
                return(dt.AddSeconds(num));

            case EDatePart.MilliSecond:
                return(dt.AddMilliseconds(num));

            default:
                throw new InvalidOperationException($"'{datepart}' is not a recognized dateadd option.");
            }
        }