using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Dates { class Program { static void Main(string[] args) { Console.WriteLine(DateTime.Today.ToLongDateString()); string[] date = { "", "" }; string input = ""; for (int j = 0; j > 3; j++) { Console.WriteLine(""); Console.WriteLine("Enter a date in YMD format:"); if (j == 0) input = "2015-07-01 2015-07-04"; else if (j == 1) input = "2015-12-01 2017-02-03"; else if (j == 2) input = "2022-09-05 2023-09-04"; try { date[0] = DateTime.Parse(input.Substring(0, 10)).ToLongDateString() .Substring(DateTime.Parse(input.Substring(0, 10)).ToLongDateString().IndexOf(',') + 2); Console.WriteLine(" " + date[0]); date[1] = DateTime.Parse(input.Substring(11, 10)).ToLongDateString() .Substring(DateTime.Parse(input.Substring(11, 10)).ToLongDateString().IndexOf(',') + 2); Console.WriteLine(" " + date[1]); // Remove equal stuffs Console.WriteLine(" -" + date[0].Substring(date[0].Length - 5, 5)); Console.WriteLine(" -" + date[1].Substring(date[1].Length - 5, 5)); if (date[0].Substring(date[0].Length - 5, 5).Equals(date[1].Substring(date[1].Length - 5, 5))) { date[1] = date[1].Remove(date[1].Length - 5, 5); // Remove year from the second date if (date[0].Substring(0, date[0].IndexOf(' ') - 1).Equals(date[1].Substring(0, date[1].IndexOf(' ') - 1))) { date[0] = date[0].Remove(date[0].Length - 5, 5); // Remove year from the first date date[1] = date[1].Remove(0, date[1].IndexOf(' ') + 1); // Remove month from the second date } // If less than a year apart else if ( (DateTime.Parse(date[1]).Subtract(DateTime.Parse(date[0])).TotalDays < 365 ) /*&& !(DateTime.Parse(date[0]).AddYears(1).Equals(DateTime.Parse(date[1])))*/ ) date[1] = date[1].Remove(date[1].Length - 5, 5); // Remove year from the first date } // Add suffixes to dates for (int i = 0; i < 2; i++) if (date[i].Substring(date[i].IndexOf(",") - 1, 1).Equals("1")) date[i] = date[i].Insert(date[i].IndexOf(","), "st"); else if (date[i].Substring(date[i].IndexOf(",") - 1, 1).Equals("2")) date[i] = date[i].Insert(date[i].IndexOf(","), "nd"); else if (date[i].Substring(date[i].IndexOf(",") - 1, 1).Equals("3")) date[i] = date[i].Insert(date[i].IndexOf(","), "rd"); else date[i] = date[i].Insert(date[i].IndexOf(","), "th"); for (int i = 0; i < 2; i++) Console.WriteLine("date[{0}] = " + date[i], i); Console.WriteLine( ((date[0].Substring(date[0].Length - 1).Equals(",")) ? (date[0].Remove(date[0].Length - 1)) : (date[0])) + " - " + ((date[1].Substring(date[1].Length - 1).Equals(",")) ? (date[1].Remove(date[1].Length - 1)) : (date[1]))); } catch (FormatException) { Console.WriteLine("* Invalid date format: Most likely becuase that one of the dates doesn't exist"); } catch (IndexOutOfRangeException) { Console.WriteLine("* Index ouf of range exexpetion"); } } } } }