예제 #1
0
        /// <summary>
        /// Gets the house-style description of a period from one date/time to another
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="showStartTime">if set to <c>true</c> show start time.</param>
        /// <param name="showEndTime">if set to <c>true</c> show end time.</param>
        /// <param name="useShortDateText">if set to <c>true</c> omit day name and use short month names.</param>
        /// <returns>
        /// A text string which includes HTML entities
        /// </returns>
        public static string DateRange(DateTime startDate, DateTime endDate, bool showStartTime, bool showEndTime, bool useShortDateText)
        {
            bool multiDay  = (startDate.DayOfYear != endDate.DayOfYear || startDate.Year != endDate.Year);
            bool showTime  = (showStartTime || showEndTime);
            bool sameMonth = (startDate.Month == endDate.Month && startDate.Year == endDate.Year);

            if (!multiDay && !showTime)
            {
                /*
                 *  One day, no time
                 *  ---------------------------------------
                 *  Friday 26 May 2006
                 */

                if (useShortDateText)
                {
                    return(ShortBritishDate(startDate));
                }
                return(FullBritishDateWithDay(startDate));
            }
            else if (!multiDay && showStartTime && !showEndTime)
            {
                /*
                 *  One day, with start time
                 *  ---------------------------------------
                 *  9am, Friday 26 May 2006
                 */
                if (useShortDateText)
                {
                    return(ShortBritishDateWithTime(startDate));
                }
                return(DateTimeFormatter.FullBritishDateWithDayAndTime(startDate));
            }
            else if (!multiDay && showStartTime && showEndTime)
            {
                /*
                 *  One day, with start and finish times
                 *  ---------------------------------------
                 *  9am-2pm, Friday 26 May 2006
                 */
                if (useShortDateText)
                {
                    return(new StringBuilder(DateTimeFormatter.Time(startDate)).Append(" to ").Append(DateTimeFormatter.Time(endDate)).Append(", ").Append(DateTimeFormatter.ShortBritishDate(startDate)).ToString());
                }
                return(new StringBuilder(DateTimeFormatter.Time(startDate)).Append(" to ").Append(DateTimeFormatter.Time(endDate)).Append(", ").Append(DateTimeFormatter.FullBritishDateWithDay(startDate)).ToString());
            }
            else if (multiDay && !showTime && sameMonth)
            {
                /*
                 *  Different days, no times, same month
                 *  ---------------------------------------
                 *  26-27 May 2006
                 */
                if (useShortDateText)
                {
                    return(new StringBuilder(startDate.Day.ToString(CultureInfo.CurrentCulture)).Append(" to ").Append(DateTimeFormatter.ShortBritishDate(endDate)).ToString());
                }
                return(new StringBuilder(startDate.Day.ToString(CultureInfo.CurrentCulture)).Append(" to ").Append(DateTimeFormatter.FullBritishDate(endDate)).ToString());
            }
            else if (multiDay && !showTime && !sameMonth)
            {
                /*
                 *  Different days, no times, different month
                 *  ---------------------------------------
                 *  Friday 26 May 2006 - Thursday 1 June 2006
                 */
                if (useShortDateText)
                {
                    return(new StringBuilder(DateTimeFormatter.ShortBritishDate(startDate)).Append(" to ").Append(DateTimeFormatter.ShortBritishDate(endDate)).ToString());
                }
                return(new StringBuilder(DateTimeFormatter.FullBritishDateWithDay(startDate)).Append(" to ").Append(DateTimeFormatter.FullBritishDateWithDay(endDate)).ToString());
            }
            else if (multiDay && showStartTime && !showEndTime)
            {
                /*
                 *  Different days, with start time
                 *  ---------------------------------------
                 *  9am, Friday 26 May 2006 to Saturday 27 May 2006
                 */
                if (useShortDateText)
                {
                    return(new StringBuilder(DateTimeFormatter.ShortBritishDateWithTime(startDate)).Append(" to ").Append(DateTimeFormatter.ShortBritishDate(endDate)).ToString());
                }
                return(new StringBuilder(DateTimeFormatter.FullBritishDateWithDayAndTime(startDate)).Append(" to ").Append(DateTimeFormatter.FullBritishDateWithDay(endDate)).ToString());
            }
            else if (multiDay && showStartTime && showEndTime)
            {
                /*
                 *  Different days, with start and end time
                 *  ---------------------------------------
                 *  9am, Friday 26 May 2006 to 2pm, Saturday 27 May 2006
                 */
                if (useShortDateText)
                {
                    return(new StringBuilder(DateTimeFormatter.ShortBritishDateWithTime(startDate)).Append(" to ").Append(DateTimeFormatter.ShortBritishDateWithTime(endDate)).ToString());
                }
                return(new StringBuilder(DateTimeFormatter.FullBritishDateWithDayAndTime(startDate)).Append(" to ").Append(DateTimeFormatter.FullBritishDateWithDayAndTime(endDate)).ToString());
            }

            // Shouldn't get here
            return(String.Empty);
        }
예제 #2
0
 /// <summary>
 /// Get a string in the format 10am, 1 Jan 2004 from a DateTime object
 /// </summary>
 public static string ShortBritishDateWithTime(DateTime date)
 {
     return(new StringBuilder(DateTimeFormatter.ShortBritishDate(date)).Append(", ").Append(DateTimeFormatter.Time(date)).ToString());
 }