/// <summary> /// Gets the house-style description of a period from one date and time to another, with HTML controls which implement the hCalendar microformat /// </summary> /// <param name="startDate">The start date.</param> /// <param name="endDate">The end date.</param> /// <returns>If the date can be presented as a single string, a single control is returned. If the start and finish dates should be presented separately, two controls will be returned.</returns> /// <remarks>These controls form only one part of the hCalendar microformat, and will not work unless other parts of the format are also implemented.</remarks> public static Control[] DateRangeHCalendar(DateTime startDate, DateTime endDate) { return(DateTimeFormatter.DateRangeHCalendar(startDate, endDate, true, true, true, true)); }
/// <summary> /// Gets the house-style description of a period from one date and time to another, with HTML controls which implement the hCalendar microformat /// </summary> /// <param name="startDate">The start date.</param> /// <param name="endDate">The end date.</param> /// <param name="startTimeKnown">if set to <c>true</c> the supplied start time is correct. If <c>false</c>, 00.00 is assumed.</param> /// <param name="endTimeKnown">if set to <c>true</c> the supplied end time is correct. If <c>false</c> 00.00 the next morning is assumed.</param> /// <param name="showStartTime">if set to <c>true</c> display the start time.</param> /// <param name="showEndTime">if set to <c>true</c> display the end time.</param> /// <returns>HTML controls with the date embedded</returns> public static Control[] DateRangeHCalendar(DateTime startDate, DateTime endDate, bool startTimeKnown, bool endTimeKnown, bool showStartTime, bool showEndTime) { bool multiDay = (startDate.DayOfYear != endDate.DayOfYear || startDate.Year != endDate.Year); bool showTime = (showStartTime || showEndTime); bool sameMonth = (startDate.Month == endDate.Month && startDate.Year == endDate.Year); // The display date may need to be different to the metadata (actual) date, so work with two copies DateTime displayStartDate = startDate; DateTime displayEndDate = endDate; if (!startTimeKnown) { startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day); } if (!startTimeKnown || !showStartTime) { displayStartDate = new DateTime(displayStartDate.Year, displayStartDate.Month, displayStartDate.Day); } // all-day event goes up to midnight at the start of the NEXT day if (!endTimeKnown) { endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day); } if (!endTimeKnown || !showEndTime) { displayEndDate = new DateTime(displayEndDate.Year, displayEndDate.Month, displayEndDate.Day); } HtmlGenericControl tagStart = new HtmlGenericControl("time"); tagStart.Attributes["class"] = "dtstart"; // hCalendar tagStart.Attributes["datetime"] = DateTimeFormatter.Iso8601DateTime(startDate); // hCalendar HtmlGenericControl tagEnd = new HtmlGenericControl("time"); tagEnd.Attributes["class"] = "dtend"; // hCalendar tagEnd.Attributes["datetime"] = DateTimeFormatter.Iso8601DateTime(endDate); // hCalendar if (!multiDay && !showTime) { /* * One day, no time * --------------------------------------- * Friday 26 May 2006 */ Control[] hCal = new Control[1]; PlaceHolder ph = new PlaceHolder(); hCal[0] = ph; ph.Controls.Add(tagStart); ph.Controls.Add(tagEnd); tagEnd.InnerText = DateTimeFormatter.FullBritishDateWithDay(displayStartDate); return(hCal); } else if (!multiDay && ((showStartTime && !showEndTime) || (showStartTime && showEndTime && displayStartDate == displayEndDate))) { /* * One day, with start time (or matching start and end times) * --------------------------------------- * 9am, Friday 26 May 2006 */ Control[] hCal = new Control[1]; PlaceHolder ph = new PlaceHolder(); hCal[0] = ph; ph.Controls.Add(tagStart); ph.Controls.Add(tagEnd); tagEnd.InnerText = DateTimeFormatter.FullBritishDateWithDayAndTime(displayStartDate); return(hCal); } else if (!multiDay && showStartTime && showEndTime) { /* * One day, with start and finish times * --------------------------------------- * 9am to 2pm, Friday 26 May 2006 */ Control[] hCal = new Control[1]; PlaceHolder ph = new PlaceHolder(); hCal[0] = ph; tagStart.InnerText = DateTimeFormatter.Time(displayStartDate); ph.Controls.Add(tagStart); ph.Controls.Add(new LiteralControl(" to ")); tagEnd.InnerText = (new StringBuilder(DateTimeFormatter.Time(displayEndDate)).Append(", ").Append(DateTimeFormatter.FullBritishDateWithDay(displayStartDate)).ToString()); ph.Controls.Add(tagEnd); return(hCal); } else if (multiDay && !showTime && sameMonth) { /* * Different days, no times, same month * --------------------------------------- * 26 to 27 May 2006 */ Control[] hCal = new Control[1]; PlaceHolder ph = new PlaceHolder(); hCal[0] = ph; tagStart.InnerText = displayStartDate.Day.ToString(CultureInfo.CurrentCulture); ph.Controls.Add(tagStart); ph.Controls.Add(new LiteralControl(" to ")); tagEnd.InnerText = DateTimeFormatter.FullBritishDate(displayEndDate); ph.Controls.Add(tagEnd); return(hCal); } else if (multiDay && !showTime && !sameMonth) { /* * Different days, no times, different month * --------------------------------------- * Friday 26 May 2006 to Thursday 1 June 2006 */ Control[] hCal = new Control[1]; PlaceHolder ph = new PlaceHolder(); hCal[0] = ph; tagStart.InnerText = DateTimeFormatter.FullBritishDateWithDay(displayStartDate); ph.Controls.Add(tagStart); // Previous version using hyphen (for reference): ph.Controls.Add(new LiteralControl(" – ")); ph.Controls.Add(new LiteralControl(" to ")); tagEnd.InnerText = DateTimeFormatter.FullBritishDateWithDay(displayEndDate); ph.Controls.Add(tagEnd); return(hCal); } else if (multiDay && showStartTime && !showEndTime) { /* * Different days, with start time * --------------------------------------- * Start: 9am, Friday 26 May 2006 * Finish: Saturday 27 May 2006 */ Control[] hCal = new Control[2]; hCal[0] = tagStart; hCal[1] = tagEnd; tagStart.InnerText = DateTimeFormatter.FullBritishDateWithDayAndTime(displayStartDate); tagEnd.InnerText = DateTimeFormatter.FullBritishDateWithDay(displayEndDate); return(hCal); } else if (multiDay && showStartTime && showEndTime) { /* * Different days, with start and end time * --------------------------------------- * Start: 9am, Friday 26 May 2006 * Finish: 2pm, Saturday 27 May 2006 */ Control[] hCal = new Control[2]; hCal[0] = tagStart; hCal[1] = tagEnd; tagStart.InnerText = DateTimeFormatter.FullBritishDateWithDayAndTime(displayStartDate); tagEnd.InnerText = DateTimeFormatter.FullBritishDateWithDayAndTime(displayEndDate); return(hCal); } // Shouldn't get here return(new Control[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); }
/// <summary> /// Gets the house-style description of a period from one date and time to another /// </summary> /// <param name="startDate">The start date.</param> /// <param name="endDate">The end date.</param> /// <returns>A text string which includes HTML entities</returns> public static string DateRange(DateTime startDate, DateTime endDate) { return(DateTimeFormatter.DateRange(startDate, endDate, true, true)); }
/// <summary> /// Get a string in the format 1 Jan, 10am. Use only for short-term data about the current year, never for anything which will be seen later on. /// </summary> public static string ShortBritishDateNoYearWithTime(DateTime date) { return(new StringBuilder(DateTimeFormatter.ShortBritishDateNoYear(date)).Append(", ").Append(DateTimeFormatter.Time(date)).ToString()); }
/// <summary> /// Get a string in the format 10am, Monday 1 January 2004 from a DateTime object /// </summary> public static string FullBritishDateWithDayAndTime(DateTime date) { return(new StringBuilder(DateTimeFormatter.FullBritishDateWithDay(date)).Append(", ").Append(DateTimeFormatter.Time(date)).ToString()); }
/// <summary> /// Takes a string as a date and returns a string in full british format with date and time /// </summary> /// <param name="date"></param> /// <returns></returns> public static string FullBritishDateWithDayAndTimeString(string date) { return(DateTimeFormatter.FullBritishDateWithDayAndTime(DateTime.Parse(date, CultureInfo.CurrentCulture))); }