Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dates in shorthand

Tags:

c#

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.

like image 792
Rod Avatar asked Oct 25 '22 00:10

Rod


1 Answers

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
like image 56
Pauli Østerø Avatar answered Nov 15 '22 06:11

Pauli Østerø