0

I have an app which collects data from a Google API. When im sending a query to the API it only accepts date as string format yyyy-MM-dd and that's fine. For that reason I'm converting this DateTime variable to string format yyyy-MM-dd so the API accepts my request. I'm parsing DateTime to string as this will be a part of a large MVC application and I would like to stick with DateTime regarding data that concerns dates. In the case below start will return a date like eg: 2014-03-20 when I'm sending the request to the API.

Console.WriteLine("Enter StartDate! (yyyy-MM-dd)");     
var start = Console.ReadLine();
var StartDate = DateTime.ParseExact(start, "yyyy-MM-dd", CultureInfo.InvariantCulture);

I would like to sort the dates that are returned by year, start will return StartDates as string yyyy-MM-dd from a list of dates. Somehow i need to apply some logic saying yyyy = year. Is there a standard procedure for this?

Abbas
  • 14,186
  • 6
  • 41
  • 72
WhoAmI
  • 1,188
  • 6
  • 17
  • 47
  • Are you sorting a collection of DateTime objects or a collection of strings? – Troy Carlson Mar 20 '14 at 16:11
  • its a collection of strings, simply a list of "Start" and "End" as strings. – WhoAmI Mar 20 '14 at 16:13
  • 7
    If your are in `yyyy-MM-dd` format, you can just sort the usual way, this will sort by year, then by month, then by day – Chris Ballard Mar 20 '14 at 16:14
  • I am probably missing something but if you are just asking how to sort dates as string in yyyy-MM-dd format then just as Chris Ballard says sort them normaly like any of these.. http://dotnetfiddle.net/ELMJKe – Karl-Henrik Mar 20 '14 at 16:33
  • Consider parsing the strings with your mentioned format, converting it to Unix Timestamps, and store. Much easier to sort this way. Example: http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa I personally use Jon Skeet's NodaTime library. It's a great tool, especially once you get into dates where it involves timezones, which the C# `DateTime` library begins to become *very* confusing. – theGreenCabbage Mar 20 '14 at 16:54
  • @theGreenCabbage - yes, but not relevant here. :) – Matt Johnson-Pint Mar 21 '14 at 02:29
  • @KristofferAndersson - You're making sense right up to "I would like to sort the dates..." - up till then, you have only shown code for taking a single date from your user. What is it that you're *sorting*? – Matt Johnson-Pint Mar 21 '14 at 02:30
  • Alright, guess this would be fairly uncomplicated then. Thx – WhoAmI Mar 21 '14 at 12:13

0 Answers0