Tuesday, July 1, 2014

DateTime getting the duration from each element within two lists(start time/end time)

Try this



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> list1 = new List<string> {
"Feb 12 01:23:57",
"Feb 12 01:25:50",
"Feb 12 01:28:57"
};
List<string> list2 = new List<string> {
"Feb 12 02:23:57",
"Feb 12 03:25:50",
"Feb 12 04:28:57",
"Feb 12 08:45:50"
};
CultureInfo provider = CultureInfo.InvariantCulture;
List<DateTime> tlist1 = list1.Select(x => DateTime.ParseExact(x, "MMM dd hh:mm:ss", provider)).ToList();
List<DateTime> tlist2 = list2.Select(x => DateTime.ParseExact(x, "MMM dd hh:mm:ss", provider)).ToList();
List<TimeSpan> results = null;
if (tlist1.Count <= tlist2.Count)
results = tlist1.Select((x, index) => tlist2[index].Subtract(x)).ToList();
else
results = tlist2.Select((x, index) => x.Subtract(tlist1[index])).ToList();
}
}
}





jdweng


No comments:

Post a Comment