0

I am getting 2 strings back from an API one for the date, and one for the time. I need to convert the 2 of them into a single DateTime object with the date and time in it. the 2 strings I get back are

Date: "2017-10-17" 
Time: "8:00PM" 

Below is what I am trying to do and I can not get it to work. If I remove the time from it I can parse just the date, but I cant seem to figure out the pattern to add the time in it.

string date = "2017-10-17";
string time = "8:00PM"'

string startTime = $"{date} {time}";

DateTime date = DateTime.ParseExact(startTime, "yyyy-MM-dd hh:mmtt", System.Globalization.CultureInfo.CurrentCulture);

How can I get those 2 strings to combine into a single DateTime object?

Brandon Turpy
  • 883
  • 1
  • 10
  • 32
  • Just guessing, but probably need to use format of "yyyy-MM-dd h:mmtt" instead. – Glorin Oakenfoot Nov 18 '17 at 03:24
  • I HATE when its something so simple I just overlook! Works now, and not sure how I did not see that before asking a dumb question! :) – Brandon Turpy Nov 18 '17 at 03:25
  • just `DateTime date = DateTime.Parse(date + " " + time)` – Slai Nov 18 '17 at 03:27
  • Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly. So use one d not dd – CodingYoshi Nov 18 '17 at 03:30

1 Answers1

0
string date = "2017-10-17";
string time = "8:00PM";

string startTime = $"{date} {time}";
DateTime myDate = DateTime.ParseExact(startTime, "yyyy-MM-dd h:mmtt",System.Globalization.CultureInfo.InvariantCulture);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396