Home > Blockchain >  convert separate date and time strings to datetime c#
convert separate date and time strings to datetime c#

Time:06-11

I have a csv table with two cells: Date and time:

objectID Time Name Date
0 07:22 AM Bonbon 2022-03-14
1 03:15 PM Pie 2022-03-23

I am trying to combine the date time such as: DateTime bonbonTime = "2022-03-14 07:22 AM" I could just do that like such:

string bonbonTimeString = 
    table.GetCell(searchIndex, Headers.Time) 
      " " 
      table.GetCell(searchIndex, Headers.Date)
DateTime bonbonTime = DateTime.ParseExact(bonbonTimeString, "yyyy-MM-dd t", CultureInfo.InvariantCulture);

However, as this is in a loop, I try to avoid too many string conversions. So I tried this:

DateTime date = DateTime.Parse(table.GetCell(searchIndex, Headers.Date));
DateTime time = DateTime.Parse(table.GetCell(searchIndex, Headers.Time));
DateTime dateTime = date.AddTicks(time.Ticks);

Turns out that time won't equal to 7:22 AM but to 2022-06-10 07:22 AM (today) so the end result is in year 2044. What would be the performance efficient way of doing that?

CodePudding user response:

You can't mix custom and standard format strings, so "yyyy-MM-dd t" will not work as your format. You need to specify the time format exactly:

DateTime.ParseExact(bonbonTimeString, "yyyy-MM-dd hh:mm tt", CultureInfo.InvariantCulture);

CodePudding user response:

I personally would stick with the concatenation (assuming the dates and times are always in the same format), just because this way it will only need to be parsed once. I would do something like the following:

string date = table.GetCell(searchIndex, Headers.Date);
string time = table.GetCell(searchIndex, Headers.Time);
DateTime dateTime = DateTime.Parse($"{date} {time}");

If you don't want to do it this way and would rather splice two datetimes together, you could get your current example to work just by subtracting DateTime.Today (the current date at midnight) from your time which will make it a TimeSpan representing the time you need to add to your date. This would look something like the following:

DateTime date = DateTime.Parse(table.GetCell(searchIndex, Headers.Date));
DateTime timeToday = DateTime.Parse(table.GetCell(searchIndex, Headers.Time));
TimeSpan time = timeToday - DateTime.Today;
DateTime dateTime = date   time;

Neither of these methods are insanely performance intensive, so it really won't make much of a difference which one you use. However, I think first one is very slightly faster just because it only needs to parse the string once.

CodePudding user response:

You should parse the time part as a TimeSpan and then add the resulting value to the date.

DateTime date = DateTime.Parse(table.GetCell(searchIndex, Headers.Date));
TimeSpan time = TimeSpan.Parse(table.GetCell(searchIndex, Headers.Time));
DateTime dateTime = date.Add(time);

The current code converts the Time in a DateTime, but being the result a DateTime then the Ticks property counts also the Date part.

  • Related