-3

I have 3 separate strings with the following format:

01-29-2016: a date, picked from a bootstrap datepicker

1:00am start time, picked from a dropdown, format could also be e.g. 10:00pm

2:30am end time, picked from a dropdown, format could also be e.g. 11:30pm

Using the above strings I need to construct 2 DateTime properties that represent the time range, something like below:

2016-01-29 02:30:00

2016-01-29 01:00:00

I need the DateTime properties so I could update datetime database fields

alex
  • 1,300
  • 1
  • 29
  • 64

1 Answers1

3

You can combine both your time parts with date part respectively and use ParseExact method with MM-dd-yyyyH:mmtt format like;

var date = "01-29-2016";
var ts1 = "1:00am";
var ts2 = "2:30am";

var dt1 = DateTime.ParseExact(date + ts1, "MM-dd-yyyyH:mmtt", CultureInfo.InvariantCulture);
// 29.01.2016 01:00:00
var dt2 = DateTime.ParseExact(date + ts2, "MM-dd-yyyyH:mmtt", CultureInfo.InvariantCulture);
// 29.01.2016 02:30:00
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364