Example #1
0
        /// <summary>
        /// Compares 2 dates and determine the time interval between them taking time zones into account.
        /// </summary>
        /// <example>
        /// For example, if using "age" for comparison, startDate = "2000-02-29 00:00:00.000", and endDate = "2010-2-28 23:30:00:00.000",
        /// and the startDate timezone is CST the return value is 9.
        /// <code>
        /// using Mezzocode.Halide3;
        /// ...
        /// double result = h3Temporal.DateDiff(
        ///		h3Temporal.DateDiffComparisonType.days,
        ///		startDate,
        ///		endDate);
        /// </code>
        /// </example>
        /// <param name="howtocompare">String specifying compare type</param>
        /// <param name="startDate">First date for comparison. If earlier than endDate, a positive result is returned.</param>
        /// <param name="startDateOffset">UTC offset value for startDate datetime.</param>
        /// <param name="endDate">Last date for comparison. If later than startDate, a positive result is returned.</param>
        /// <param name="endDateOffset">UTC offset value for endDate datetime.</param>
        /// <returns></returns>
        public static Double DateDiff(DateDiffComparisonType howtocompare, System.DateTime startDate, int startDateOffset, System.DateTime endDate, int endDateOffset)
        {
            double diff = 0;

            startDate = (new DateTimeOffset(startDate, new TimeSpan(startDateOffset, 0, 0))).UtcDateTime;
            endDate = (new DateTimeOffset(endDate, new TimeSpan(endDateOffset, 0, 0))).UtcDateTime;

            try
            {
                #region Non-Fractional conversion options
                if (howtocompare == DateDiffComparisonType.Days)
                {

                    DateTime sd = new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0, 0);
                    DateTime ed = new DateTime(endDate.Year, endDate.Month, endDate.Day, 0, 0, 0);

                    System.TimeSpan TS = new System.TimeSpan(ed.Ticks - sd.Ticks);

                    diff = Convert.ToDouble(TS.TotalDays);

                }
                else if (howtocompare == DateDiffComparisonType.age)
                {
                    int age = endDate.Year - startDate.Year;    //people perceive their age in years
                    if (endDate.Month < startDate.Month || ((endDate.Month == startDate.Month) && (endDate.Day < startDate.Day)))
                    {
                        age--;  //birthday in current year not yet reached, so we are 1 year younger
                        //note that this structure explicitly places March 1st as the non-leapyear birthday for those born on Feb 29th.
                    }
                    diff = Convert.ToDouble(age);
                }
                #endregion
                else
                {
                    System.TimeSpan TS = new System.TimeSpan(endDate.Ticks - startDate.Ticks);

                    #region Fractional conversion options

                    switch (howtocompare)
                    {
                        case DateDiffComparisonType.minutes:
                            diff = Convert.ToDouble(TS.TotalMinutes);
                            break;

                        case DateDiffComparisonType.hours:
                            diff = Convert.ToDouble(TS.TotalMinutes / 60);
                            break;

                        case DateDiffComparisonType.seconds:
                            diff = Convert.ToDouble(TS.TotalSeconds);
                            break;

                        case DateDiffComparisonType.ticks:
                            diff = Convert.ToDouble(TS.Ticks);
                            break;

                        case DateDiffComparisonType.milliseconds:
                            diff = Convert.ToDouble(TS.TotalMilliseconds);
                            break;

                        case DateDiffComparisonType.years:
                            diff = Convert.ToDouble(TS.TotalDays / 365.255);
                            break;

                        case DateDiffComparisonType.quarters: //TO DO: not use a calculation, but instead use Jan 1, Apr 1, July 1, Oct 1 to determine current quarter of this year (and how many quarters of the initial year) and add the remaining years as 4 quarters each
                            diff = Convert.ToDouble((TS.TotalDays / 365.255) / 4);
                            break;

                        default:
                            diff = Convert.ToDouble(TS.TotalDays); break;
                    }

                    #endregion
                }
            }

            catch
            {
                diff = -1;
            }

            return diff;
        }
Example #2
0
        /// <summary><![CDATA[
        /// Compares 2 dates and determine the time interval between them taking time zones into account.
        /// ]]></summary>
        /// <example>
        /// For example, if using "age" for comparison, startDate = "2000-02-29 00:00:00.000", and endDate = "2010-2-28 23:30:00:00.000",
        /// and the startDate timezone is CST the return value is 9.
        /// </example>
        /// <param name="howtocompare">String specifying compare type</param>
        /// <param name="startDate">First date for comparison. If earlier than endDate, a positive result is returned.</param>
        /// <param name="startDateOffset">UTC offset value for startDate datetime.</param>
        /// <param name="endDate">Last date for comparison. If later than startDate, a positive result is returned.</param>
        /// <param name="endDateOffset">UTC offset value for endDate datetime.</param>
        /// <returns></returns>
        public static T DateDiff <T>(this DateTime startDate, DateTime endDate, DateDiffComparisonType howtocompare, int startDateOffset = 0, int endDateOffset = 0)
        {
            double diff = 0;

            if (startDate.Kind != DateTimeKind.Utc)
            {
                startDate = (new DateTimeOffset(startDate, new TimeSpan(startDateOffset, 0, 0))).UtcDateTime;
            }

            if (endDate.Kind != DateTimeKind.Utc)
            {
                endDate = (new DateTimeOffset(endDate, new TimeSpan(endDateOffset, 0, 0))).UtcDateTime;
            }

            try
            {
                #region Non-Fractional conversion options

                if (howtocompare == DateDiffComparisonType.DaysWhole)
                {
                    DateTime sd = new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0, 0);
                    DateTime ed = new DateTime(endDate.Year, endDate.Month, endDate.Day, 0, 0, 0);

                    System.TimeSpan TS = new System.TimeSpan(ed.Ticks - sd.Ticks);

                    diff = Convert.ToDouble(TS.TotalDays);
                }

                else if (howtocompare == DateDiffComparisonType.Age)
                {
                    int age = endDate.Year - startDate.Year;    //people perceive their age in years

                    if (endDate.Month < startDate.Month || ((endDate.Month == startDate.Month) && (endDate.Day < startDate.Day)))
                    {
                        age--;  //birthday in current year not yet reached, so we are 1 year younger
                                //note that this structure explicitly places March 1st as the non-leapyear birthday for those born on Feb 29th.
                    }

                    diff = Convert.ToDouble(age);
                }

                #endregion

                else
                {
                    System.TimeSpan TS = new System.TimeSpan(endDate.Ticks - startDate.Ticks);

                    #region Fractional conversion options

                    switch (howtocompare)
                    {
                    case DateDiffComparisonType.Minutes:
                        diff = Convert.ToDouble(TS.TotalMinutes);
                        break;

                    case DateDiffComparisonType.Hours:
                        diff = Convert.ToDouble(TS.TotalHours);
                        break;

                    case DateDiffComparisonType.Seconds:
                        diff = Convert.ToDouble(TS.TotalSeconds);
                        break;

                    case DateDiffComparisonType.Ticks:
                        diff = Convert.ToDouble(TS.Ticks);
                        break;

                    case DateDiffComparisonType.Milliseconds:
                        diff = Convert.ToDouble(TS.TotalMilliseconds);
                        break;

                    case DateDiffComparisonType.Months:
                        diff = Convert.ToDouble(TS.TotalDays / 30.438);
                        break;

                    case DateDiffComparisonType.Years:
                        diff = Convert.ToDouble(TS.TotalDays / 365.255);
                        break;

                    case DateDiffComparisonType.Quarters:     //TO DO: not use a calculation, but instead use Jan 1, Apr 1, July 1, Oct 1 to determine current quarter of this year (and how many quarters of the initial year) and add the remaining years as 4 quarters each
                        diff = Convert.ToDouble((TS.TotalDays / 365.255) / 4);
                        break;

                    default:
                        diff = Convert.ToDouble(TS.TotalDays); break;
                    }

                    #endregion
                }
            }

            catch
            {
                diff = -1;
            }

            return((T)Convert.ChangeType(diff, typeof(T)));
        }
Example #3
0
 /// <summary>
 /// Compares 2 dates and determine the time interval between them.
 /// </summary>
 /// <example>
 /// For example, if using "Days" for comparison, startDate = "1/1/2008", and endDate = "1/7/2008",
 /// the return value is 6. Likewise, reversing the dates yields -6.
 /// <code>
 /// using Mezzocode.Halide3;
 /// ...
 /// double result = h3Temporal.DateDiff(
 ///		h3Temporal.DateDiffComparisonType.days,
 ///		startDate,
 ///		endDate);
 /// </code>
 /// </example>
 /// <param name="howtocompare">String specifying comparison type.</param>
 /// <param name="startDate">First date for comparison. If earlier than endDate, a positive result is returned.</param>
 /// <param name="endDate">Last date for comparison. If later than startDate, a positive result is returned.</param>
 /// <returns>-1 on failure, or the date difference as a double.</returns>
 public static Double DateDiff(DateDiffComparisonType howtocompare, System.DateTime startDate, System.DateTime endDate)
 {
     return DateDiff(howtocompare, startDate, TimeZone.CurrentTimeZone.GetUtcOffset(startDate).Hours, endDate, TimeZone.CurrentTimeZone.GetUtcOffset(endDate).Hours);
 }
Example #4
0
 /// <summary><![CDATA[
 /// Compares 2 dates and determine the time interval between them.
 /// ]]></summary>
 /// <example>
 /// For example, if using "Days" for comparison, startDate = "1/1/2008", and endDate = "1/7/2008",
 /// the return value is 6. Likewise, reversing the dates yields -6.
 /// </example>
 /// <param name="howtocompare">String specifying comparison type.</param>
 /// <param name="startDate">First date for comparison. If earlier than endDate, a positive result is returned.</param>
 /// <param name="endDate">Last date for comparison. If later than startDate, a positive result is returned.</param>
 /// <returns>-1 on failure, or the date difference as a double.</returns>
 public static T DateDiff <T>(this DateTime startDate, DateTime endDate, DateDiffComparisonType howtocompare)
 {
     return(DateDiff <T>(startDate, endDate, howtocompare, TimeZone.CurrentTimeZone.GetUtcOffset(startDate).Hours, TimeZone.CurrentTimeZone.GetUtcOffset(endDate).Hours));
 }