How to find Sum of DateTime using LINQ - c#

I have a collection of class with a DateTime Property, I would like to get sum of Minutes and Seconds. Is there any easy way to do this using LINQ without manually summing the minutes and seconds?

I suppose you mean TimeSpan instead of DateTime? You can't add DateTime's...
To sum TimeSpan's:
list.Sum(span => span.TotalSeconds) ==> total seconds
TimeSpan.FromSeconds(...) ==> Convert total seconds back to time span
You can then use properties in TimeSpan to reformat it back to hours, minutes, seconds etc.

+1 for Stephen's answers.
Just for fun... if you really do want "to get sum of Minutes and Seconds" then:
class MyClass
{
public DateTime DateTimeMember {get;set;}
// other stuff
}
var myObjects = new List<MyClass>();
// fill list...
// 3 possible things you might be interested in
var myMinuteSum = myObjects.Sum(x => x.DateTimeMember.Minute);
var mySecondSum = myObjects.Sum(x => x.DateTimeMember.Second);
var myOddTotalOfMinutesAndSecondsInSeconds = myObjects.Sum(x => x.DateTimeMember.Minute * 60 + x.DateTimeMember.Second);

Related

c# Create list of selected time intervals

I'm trying to create a downtown that will display times from 00:00 to 24:59 at a selected number of intervals (and this could change from 5 minutes to 10 minutes etc
so for example a list of
00:10
00:20
00:30
or could be
00:15
00:30
I'm using the following which works, but only for a selected number of iterations (33):
List<string> query = Enumerable
.Range(0, 33)
.Select(i => DateTime.Today
.AddHours(0)
.AddMinutes(i * (double)IntervalParameter)
.ToString())
.ToList();
*IntervalParameter = 10 for the example above.
I'm looking to adapt this so it runs the full 24 hours time frame. Just looking for the most efficient way to do this.
Why not compute the number of items?
int IntervalParameter = 5;
// .Range(0, 1440 / IntervalParameter) - see Zohar Peled's comment -
// is brief, but less readable
List<string> query = Enumerable
.Range(0, (int) (new TimeSpan(24, 0, 0).TotalMinutes / IntervalParameter))
.Select(i => DateTime.Today
.AddMinutes(i * (double)IntervalParameter) // AddHours is redundant
.ToString("HH:mm")) // Let's provide HH:mm format
.ToList();
Something like this?
public static IEnumerable<TimeSpan> Intervals(TimeSpan inclusiveStart, TimeSpan exclusiveEnd, TimeSpan increment)
{
for (var time = inclusiveStart; time < exclusiveEnd; time += increment)
yield return time;
}
Example usage:
foreach (var time in Intervals(TimeSpan.Zero, TimeSpan.FromDays(1), TimeSpan.FromMinutes(15)))
{
Console.WriteLine(time);
}

How can I average a DateTime field with a LINQ query?

I have a database that has the following records in a DateTime field:
2012-04-13 08:31:00.000
2012-04-12 07:53:00.000
2012-04-11 07:59:00.000
2012-04-10 08:16:00.000
2012-04-09 15:11:00.000
2012-04-08 08:28:00.000
2012-04-06 08:26:00.000
I want to run a linq to sql query to get the average time from the records above. I tried the following:
(From o In MYDATA Select o.SleepTo).Average()
Since "SleepTo" is a datetime field I get an error on Average(). If I was trying to get the average of say an integer, the above linq query works.
What do I need to do to get it to work for datetimes?
Internally, every DateTime is really stored as a number of ticks. The Ticks property of a DateTime is defined as the "number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001." (See http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx)
You can convert the DateTimes to ticks, then average, then convert back to a datetime.
var averageTicks = (long) dates.Select(d => d.Ticks).Average();
var averageDate = new DateTime(averageTicks);
Using your data structures and formatting, it would look like this:
var averageTicks = (long)(from o in MYDATA select o.SleepTo.Ticks).Average();
var averageDate = new DateTime(averageTicks);
If you want to get the average time of each SleepTo value (ignoring the Date component), you can get the ticks of just the time:
var averageTicks = (long)(from o in MYDATA select o.SleepTo.TimeOfDay.Ticks).Average();
var averageTime = new TimeSpan(averageTicks);
Here are a couple extensions methods that can help with this... There is a core problem where if you have a lot of DateTimes in a list the LINQ average of a the ticks (long vars) will overflow.
public static long Average(this IEnumerable<long> longs)
{
long count = longs.Count();
long mean = 0;
foreach (var val in longs)
{
mean += val / count;
}
return mean;
}
public static DateTime Average(this IEnumerable<DateTime> dates)
{
return new DateTime(dates.Select(x => x.Ticks).Average());
}
The database LINQ provider doesn't seem to understand how to do averages on absolute dates. Which, if you think about it, makes sense (average is a sum divided by a count - what is the sum?).
So, if you're not able to run the following:
(From o In MYDATA Select o.SleepTo).Sum()
Then you won't be able to do .Average() also.
Since what you want is actually the average time of SleepTo, you need to get just the time component of the date as a TimeSpan (time minus midnight perhaps) and average that. Do you by any chance have SleepFrom?
In the meantime, you might find this post enlightening: LINQ Average TimeSpan?

How can I segregate properties of DateTime?

I would like to write: if the result of the difference of 2 DateTimes is longer than 3 hours then.... stuff in the if statement happens. But I only need properties in seconds or minutes, can I extract just that from the DateTime object?
if(diffResult > DateTime.Hour(3))
{
}
I also want to know if its possible to divide DateTime by periods. Say I want to split my diffResult (which is the difference between 2 DateTimes) into 3 periods or perhaps for every 3 seconds my counter gets one added to it.
For the first part:
You can subtract two DateTimes to get a TimeSpan there you can get the total of various units - for example:
if ( (secondTime - firstTime).TotalMinutes > 180.0) ...
or you could use TimeSpan directly:
if (secondTime - firstTime > TimeSpan.FromHours(3)) ...
for the secondpart you have to do some calculation yourself:
var diff = secondTime - firstTime;
var period = TimeSpan.FromSeconds(diff.TotalSeconds / 3.0);
for (var time = firstTime; time < secondTime; time += period)
{ /* do your stuff */ }
U can compare using the follow code:
DateTime dt = new DateTime();
dt = DateTime.Now;
dt.AddHours(3);
int h = (int)DateTime.Now.Hour;
if (dt.Hour == h )
//Do something
else
//do otherthing
You can do this:
TimeSpan time = new TimeSpan(3, 0, 0);
if (date1.Subtract(date2) > time)
{
//YourCode
}
For the second, this article should be useful:
http://www.blackwasp.co.uk/TimespanMultiplication.aspx
The methods your asking about return integer results. What exactly is your question? DateTime.Hour(3) would not even compile.
I think you are looking for DateTime.Now.AddHours(3.0)
I should be clear, the only reason this answer is this sparse, is because of the invalid code in the author's question which. Since I don't attempt to guess at what people actually want, its up to the author, to clarify what he wants exactly.
All he has to do is subtract two DateTime values and compare it to a TimeSpan

Find average of collection of TimeSpans

I have collection of TimeSpans, they represent time spent doing a task. Now I would like to find the average time spent on that task. It should be easy but for some reason I'm not getting the correct average.
Here's my code:
private TimeSpan? GetTimeSpanAverage(List<TimeSpan> sourceList)
{
TimeSpan total = default(TimeSpan);
var sortedDates = sourceList.OrderBy(x => x);
foreach (var dateTime in sortedDates)
{
total += dateTime;
}
return TimeSpan.FromMilliseconds(total.TotalMilliseconds/sortedDates.Count());
}
You can use the Average overload that takes a collection of long in parameter:
double doubleAverageTicks = sourceList.Average(timeSpan => timeSpan.Ticks);
long longAverageTicks = Convert.ToInt64(doubleAverageTicks);
return new TimeSpan(longAverageTicks);
var average = new TimeSpan(sourceList.Select(ts => ts.Ticks).Average());
Note, your method returns a Nullable, but doesn't need to, unless you want to return null if the source list is empty, in which case just do a separate check first.
In Addition to the above answer, I would suggest you take an average on the Seconds or MilliSeconds level (depending on what you require)
sourceList.Average(timeSpan => timeSpan.ToTalMilliseconds)
Now using this value you could arrive at the new TimeSpan using
TimeSpan avg = TimeSpan.FromMilliseconds(double value here)

Convert DateTime.Now to Seconds

I am trying to write a function that will convert a DateTime.Now instance to the number of seconds it represents so that I can compare that to another DateTime instance. Here is what I currently have:
public static int convertDateTimeToSeconds(DateTime dateTimeToConvert)
{
int secsInAMin = 60;
int secsInAnHour = 60 * secsInAMin;
int secsInADay = 24 * secsInAnHour;
double secsInAYear = (int)365.25 * secsInADay;
int totalSeconds = (int)(dateTimeToConvert.Year * secsInAYear) +
(dateTimeToConvert.DayOfYear * secsInADay) +
(dateTimeToConvert.Hour * secsInAnHour) +
(dateTimeToConvert.Minute * secsInAMin) +
dateTimeToConvert.Second;
return totalSeconds;
}
I realize that I am truncating the calculation for seconds in a year, but I don't need my calculation to be precise. I'm really looking to know if the method that I am using to calculate seconds is correct.
Does anyone have anything that could better compute seconds given from a DateTime object?
Also, Should the return type be int64 if I am coding in C# if I am going to calculate all the seconds since 0 AD?
The DateTime type supports comparison operators:
if (dateTimeA > dateTimeB)
{
...
This also works for DateTime values returned by DateTime.AddSeconds:
if (dateTimeA.AddSeconds(42) > dateTimeB)
{
...
If you really want the number of seconds that elapsed since 01/01/0001 00:00:00, you can calculate the difference between the two DateTime values. The resulting TimeSpan value has a TotalSeconds property:
double result = DateTime.Now.Subtract(DateTime.MinValue).TotalSeconds;
It really doesn't make sense to convert a DateTime object to seconds. Seconds only make sense if you are dealing with a length of time (TimeSpan). Should you want to compare two dates to get the number of seconds between them:
TimeSpan diff = DateTime.Now - PreviousDateTime;
double seconds = diff.TotalSeconds;
If the purpose is finding the number of seconds between two dates, you'd be much better off using the TimeSpan object.
TimeSpan span = date2 - date1;
double seconds = span.TotalSeconds;
See suggestion from thread below:
How do I convert ticks to minutes?
TimeSpan.FromTicks(DateTime.Now.Ticks).TotalSeconds;
Assuming you really need to get at the seconds for the datetime object, you could directly get the "Ticks" property from it. These aren't in seconds but you can easily divide by the proper factor to convert the Ticks to seconds.
See: http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx
So, something like:
DateTime.Now.Ticks/TimeSpan.TicksPerSecond
If you want to compare 2 DateTime object, why just not use the provided operators?
http://msdn.microsoft.com/en-us/library/aa326723%28v=VS.71%29.aspx
DateTime a, b;
if (a > b) //a is after b
I would use the TimeSpan class to get the exact difference between two DateTime instances. Here is an example:
DateTime dt1 = DateTime.Now;
DateTime dt2 = new DateTime(2003,4,15);
TimeSpan ts = dt1.Subtract(dt2);
Once the TimeSpan value (ts, in the code snippet above) is available, you can examine its values to correctly convert the TimeSpan to a given number of seconds.
Using a TimeSpan to get the elapsed time between two DateTimes is probably the best way to go but if you really want to get the number of seconds for a given DateTime you could do something like the following:
DateTime dateTimeToConvert = DateTime.Now;
TimeSpan tsElapsed = dateTimeToConvert - DateTime.MinValue;
return tsElapsed.TotalSeconds;
Note that tsElapsed.TotalSeconds is a Double, not an Int.
Do note that the goal is to get the number of seconds since DateTime.MinVal (the first day of the calendar). I say this, because I see all of these answers for "you do time comparisons like this... add in the object, multiply by that object and do cross-calculus on them, divide by the quotient of the summed result, and Boom! not what you asked."
There's a really simple answer here. Ticks are 100-nanosecond increments. DateTime object.Ticks is the number of ticks that have occurred since 1/1/0001. Ie, year zero. There are 10 million nanoseconds in a second. so...
public static long convertDateTimeToSeconds(DateTime dateTimeToConvert) {
// According to Wikipedia, there are 10,000,000 ticks in a second, and Now.Ticks is the span since 1/1/0001.
long NumSeconds= dateTimeToConvert.Ticks / 10000000;
return NumSeconds;
}

Categories