Let's say you have a date string like follows:
11/15/2010, 12/1/10, 12/2/10, 12/3/10, 12/4/10, 12/9/10
How would you shorten this string like follows (or something similar)
11/15/2010, 12/1-4, 9/10
Because imagine 30 days in a row it would be better to see 12/1-31/10 than have all dates listed.
Just to make it a little more compact?
Thanks, rod.
public class so_4413380
{
private class DateGroup
{
public IList<DateTime> Dates { get; set; }
public DateTime First
{
get { return Dates.OrderBy(d => d).FirstOrDefault(); }
}
public DateTime Last
{
get { return Dates.OrderBy(d => d).LastOrDefault(); }
}
public DateGroup()
{
Dates = new List<DateTime>();
}
}
public void Execute()
{
var dateTimeFormat = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
var dates = new[] { "11/15/2010", "12/1/10", "12/2/10", "12/3/10", "12/4/10", "12/9/10" };
var realDates = dates.Select(s => DateTime.Parse(s, dateTimeFormat));
var dateGroups = new List<DateGroup>();
DateTime lastDate = DateTime.MinValue;
foreach (var date in realDates.OrderBy(d => d))
{
if (date.Month == lastDate.Month && (date - lastDate).Days <= 1)
{
var dateGroup = dateGroups.LastOrDefault();
dateGroup.Dates.Add(date);
}
else
{
var dateGroup = new DateGroup();
dateGroups.Add(dateGroup);
dateGroup.Dates.Add(date);
}
lastDate = date;
}
foreach (var dateGroup in dateGroups)
{
if (dateGroup.Dates.Count == 1)
{
Console.WriteLine(dateGroup.First.ToString(dateTimeFormat.ShortDatePattern, dateTimeFormat));
}
else
{
int firstDay = dateGroup.First.Day;
int lastDay = dateGroup.Last.Day;
var dateString = dateTimeFormat.ShortDatePattern.Replace("d", firstDay + "-" + lastDay);
Console.WriteLine(dateGroup.First.ToString(dateString, dateTimeFormat));
}
}
Console.ReadLine();
}
}
Produces
11/15/2010
12/1-4/2010
12/9/2010
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With