I have run in a scenario where i need to find the total number of hours that falls on a current period. I Have a Time in and TimeOut datetime which i want to get the hours where the employee worked between 10pm-4am the following day. How would i get the output of hours worked.
I Created an Extension method like this:
public static decimal GetNightDifferentialValue(this DailyTime dtr, Employee201 employee, PayrollSettings settings, IEnumerable<Holidays> holidays)
{
//know if the time out is greater than 10pm of the dtr
//07-26-2016 14:00 - 07-27-2016 03:00
//if time out i
var days = Enumerable.Range(0, (int)(dtr.TimeOut - dtr.TimeIn).TotalHours + 1)
.Select(i => dtr.TimeIn.AddHours(i))
.Where(date => !(date.Hour >= 22)).Count();
return days* employee.Rate;
}
my problem is in the Where Method how can i Filter the hours that only fall on my category
public static decimal GetNightDifferentialValue(this DailyTime dtr, Employee201 employee, PayrollSettings settings, IEnumerable<Holidays> holidays)
{
//know if the time out is greater than 10pm of the dtr
//07-26-2016 14:00 - 07-27-2016 03:00
//if time out i
DateTime dayIn10pm = new DateTime(dtr.TimeIn.Year, dtr.TimeIn.Month, dtr.TimeIn.Day, 22, 0, 0);
DateTime dayAfter04am = dayIn10pm.Add(new TimeSpan(6,0,0));
var hours = Enumerable.Range(0, (int)(dtr.TimeOut - dtr.TimeIn).TotalHours + 1)
.Select(i => dtr.TimeIn.AddHours(i))
.Where(date => (date > dayIn10pm && date <= dayAfter04am)).Count();
return hours;
}
I see the problem is only with filtering, I would suggest compare Date part to determine is it next Date, if it is next date Look for TimeOfDay to compare Time
var t = TimeSpan.ParseExact("04:00:00", #"hh\:mm\:ss", CultureInfo.InvariantCulture);
var days = Enumerable.Range(0, (int)(dtr.TimeOut - dtr.TimeIn).TotalHours + 1)
.Select(i => dtr.TimeIn.AddHours(i))
.Where(date => (date.Date == TimeOut.Date && date.TimeOfDay <= t) || date.Hour >= 22)
.Count();
Check this Demo
I think this does what you want:
if (timeOut.Hour > 4)
{
timeOut = timeOut.Date.AddHours(4);
}
if (timeIn.Hour < 22)
{
timeIn = timeIn.Date.AddHours(22);
}
if (timeIn > timeOut)
{
// No overnight time
return 0;
}
var difference = timeOut - timeIn;
return difference.TotalHours;
Basically, we first normalize the dates:
If the employee got here before 10PM, consider he was there at 10PM
If the employee left after 4AM, consider he left at 4AM
From there, we just have to subtract the two dates, and handle the special case where the new timeIn is greater than the new timeOut (if, for instance, he worked from 2PM to 5PM). In which case it means there's no time between 10PM and 4AM, so we just return 0.
Note that this algorithm doesn't handle the case where the employee works more than 24 hours straight.
private void CalculateTotalHour(string dtstartTime, string dtendTime)
{
DateTime d1 = new DateTime();
d1 = Convert.ToDateTime(dtstartTime); DateTime d2 = new DateTime();
d2 = Convert.ToDateTime(dtendTime);
if (d1.Hour >= 12)
{
d1 = d1.AddDays(-1);
}
else if (d2.Hour >= 12)
{
d2 = d2.AddDays(1);
}
// if (d2 < d1)
// MessageBox.Show("shift end time is lesser than shift start time", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
TimeSpan ts = d2.Subtract(d1).Duration();
// ts.ToString(#"hh\:mm")//total dur
}
Get what I want but is not that good looking solution here is my solution:
var timeIn = new DateTime(2016, 7, 25, 14, 0, 0);
var timeOut = new DateTime(2016, 7, 26, 5, 0, 0);
if ((timeOut.Date - timeIn.Date).TotalDays >= 1)
{
var hrs12to4am = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
.Select(i => timeIn.AddHours(i)).Where(a => a.Hour < 4 && a.Date > timeIn).ToList();
var hrsOverTen = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
.Select(i => timeIn.AddHours(i)).Where(a => a.Hour > 22).ToList();
}
else
{
var hrsOverTen = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
.Select(i => timeIn.AddHours(i)).Where(a => a.Hour > 22).ToList();
}
here is the demo Demo
i think this is your answer
int hours = (int)(dt2 - dt1).TotalHours;
Related
I'm trying to create a function that returns the number of Dates in a Date Range that are sequential, starting on a specific date.
Example:
StartDate: 9/1/2022
Date Range: 9/1/2022, 9/2/2022, 9/3/2022, 9/4/2022, 9/7/2022
In this scenario the function I'm looking for would return 4.
Assume dates could be unordered and they can roll over into the next month, so with StartDate 9/29/2022:
9/29/2022, 9/30/2022, 10/1/2022, 10/4/2022 would return 3.
I know I can loop through the dates starting at the specific date and check the number of consecutive days, but I'm wondering if there's a clean way to do it with Linq.
This is the cleanest solution I can come up with...
var startDate = new DateTime(2022, 9, 1);
var days = new List<DateTime>()
{
new(2022, 8, 28),
new(2022, 9, 1),
new(2022, 9, 2),
new(2022, 9, 3),
new(2022, 9, 4),
new(2022, 9, 7)
};
var consecutiveDays = GetConsecutiveDays(startDate, days);
foreach (var day in consecutiveDays)
{
Console.WriteLine(day);
}
Console.ReadKey();
static IEnumerable<DateTime> GetConsecutiveDays(DateTime startDate, IEnumerable<DateTime> days)
{
var wantedDate = startDate;
foreach (var day in days.Where(d => d >= startDate).OrderBy(d => d))
{
if (day == wantedDate)
{
yield return day;
wantedDate = wantedDate.AddDays(1);
}
else
{
yield break;
}
}
}
Output is:
01.09.2022 0:00:00
02.09.2022 0:00:00
03.09.2022 0:00:00
04.09.2022 0:00:00
If you wanted the count, you can call .Count() on the result or just modify the method... Should be easy.
To count the number of consecutive dates in a given date range.
first parse the dates from a string and order them in ascending order.
Then, use the TakeWhile method to take a sequence of consecutive dates from the start of the list.
Finally, count the number of elements in the returned sequence and display the result.
public class Program
{
private static void Main(string[] args)
{
var dateRange = "9/29/2022, 9/30/2022, 10/1/2022, 10/4/2022";
var dates = dateRange
.Split(", ")
.Select(dateStr =>
{
var dateData = dateStr.Split("/");
var month = int.Parse(dateData[0]);
var day = int.Parse(dateData[1]);
var year = int.Parse(dateData[2]);
return new DateTime(year, month, day);
})
.OrderBy(x => x)
.ToList();
var consecutiveDatesCounter = dates
.TakeWhile((date, i) => i == 0 || dates[i - 1].AddDays(1) == date)
.Count();
Console.WriteLine(consecutiveDatesCounter);
}
}
Output: 3
Demo: https://dotnetfiddle.net/tYdWvz
Using a loop would probably be the cleanest way to go. I would use something like the following:
List<DateTime> GetConsecutiveDates(IEnumerable<DateTime> range, DateTime startDate)
{
var orderedRange = range.OrderBy(d => d).ToList();
int startDateIndex = orderedRange.IndexOf(startDate);
if (startDateIndex == -1) return null;
var consecutiveDates = new List<DateTime> { orderedRange[startDateIndex] };
for (int i = startDateIndex + 1; i < orderedRange.Count; i++)
{
if (orderedRange[i] != orderedRange[i - 1].AddDays(1)) break;
consecutiveDates.Add(orderedRange[i]);
}
return consecutiveDates;
}
Yet another approach using a loop. (I agree with the others that said a loop would be cleaner than using Linq for this task.)
public static int NumConsecutiveDays(IEnumerable<DateTime> dates)
{
var previous = DateTime.MinValue;
var oneDay = TimeSpan.FromDays(1);
int result = 0;
foreach (var current in dates.OrderBy(d => d))
{
if (current.Date - previous.Date == oneDay)
++result;
previous = current;
}
return result > 0 ? result + 1 : 0; // Need to add 1 to result if it is not zero.
}
var dates = new List<DateTime>() {new DateTime(2014,1,1), new DateTime(2014, 1, 2), new DateTime(2014, 1, 3) , new DateTime(2014, 1, 5), new DateTime(2014, 1, 6), new DateTime(2014, 1, 8) };
var startDate = new DateTime(2014,1,2);
var EarliestContiguousDates = dates.Where(x => x>=startDate).OrderBy(x => x)
.Select((x, i) => new { date = x, RangeStartDate = x.AddDays(-i) })
.TakeWhile(x => x.RangeStartDate == dates.Where(y => y >= startDate).Min()).Count();
You're either going to sort the dates and find sequential ones, or leave it unsorted and repeatedly iterate over the set looking for a match.
Here's the latter dumb approach, leaving it unsorted and using repeated calls to 'IndexOf`:
public static int CountConsecutiveDays(DateTime startingFrom, List<DateTime> data)
{
int count = 0;
int index = data.IndexOf(startingFrom);
while(index != -1) {
count++;
startingFrom = startingFrom.AddDays(1);
index = data.IndexOf(startingFrom);
}
return count;
}
I'm running into a scenario where I need to convert an Interval value to Enumerable collection of LocalDate in NodaTime. How can I do that?
Below is the code
Interval invl = obj.Interval;
//Here is the Interval value i.e.,{2016-10-20T00:00:00Z/2016-11-03T00:00:00Z}
How can I form a Date range between these intervals?
Thanks in advance.
A slightly alternative approach to the one given by Niyoko:
Convert both Instant values into LocalDate
Implement a range between them
I'm assuming that the interval is exclusive - so if the end point represents exactly midnight in the target time zone, you exclude that day, otherwise you include it.
So the method below includes every date which is covered within the interval, in the given time zone.
public IEnumerable<LocalDate> DatesInInterval(Interval interval, DateTimeZone zone)
{
LocalDate start = interval.Start.InZone(zone).Date;
ZonedDateTime endZonedDateTime = interval.End.InZone(zone);
LocalDate end = endLocalDateTime.Date;
if (endLocalDateTime.TimeOfDay == LocalTime.Midnight)
{
end = end.PlusDays(-1);
}
for (LocalDate date = start; date <= end; date = date.PlusDays(1))
{
yield return date;
}
}
Use this code:
var l = Enumerable.Range(0, int.MaxValue)
.Select(x => Period.FromDays(x))
.Select(x => LocalDate.Add(interval.Start.InZone(localZone).Date, x))
.TakeWhile(x => x.CompareTo(interval.End.InZone(localZone).Date) <= 0);
Example:
var localZone = DateTimeZone.ForOffset(Offset.FromHours(7));
var start = Instant.FromDateTimeOffset(new DateTimeOffset(new DateTime(2016, 10, 1)));
var end = Instant.FromDateTimeOffset(new DateTimeOffset(new DateTime(2016, 10, 25)));
var interval = new Interval(start, end);
var l = Enumerable.Range(0, int.MaxValue)
.Select(x => Period.FromDays(x))
.Select(x => LocalDate.Add(interval.Start.InZone(localZone).Date, x))
.TakeWhile(x => x.CompareTo(interval.End.InZone(localZone).Date) <= 0);
foreach (var localDate in l)
{
Console.WriteLine(localDate);
}
So I have all these travel dates, from-date and to-date. I want to add up all the travel days, and sort them by year. However if one travel for period which spans two years, my code will reach the wrong sum :-(
Given
From date To date Number of days
01.01.2001 01.02.2001 32
01.01.2002 01.02.2002 32
01.05.2002 01.08.2002 93
20.12.2002 01.03.2003 72
01.02.2009 01.02.2010 366
01.01.2013 02.02.2015 763
Sum 1358
My code produces this. However, it makes a mistake:
Year Total days
2001 32
2002 137
2003 60
2009 334
2010 32
2013 365
2014 398 <---- here is a case where my code is wrong
Sum 1358
Code
var dates = new Dictionary<int, int>();
var stays = GetStays();
var returnString = "Year, Total days<br><br>";
foreach (var stay in stays)
{
var totalTravelDays = stay.ToDate.Value.AddDays(1) - stay.FromDate;
var currentYear = stay.FromDate.Value.Year;
var nextYear = stay.FromDate.Value.AddYears(1).Year;
var nextYearDate = new DateTime(stay.FromDate.Value.Year, 1, 1).AddYears(1);
var daysInThisYear = new TimeSpan?();
var daysInNextYear = new TimeSpan?();
if (stay.FromDate.Value.Year != stay.ToDate.Value.Year)
{
daysInThisYear = nextYearDate - stay.FromDate;
daysInNextYear = totalTravelDays - daysInThisYear;
}
else
{
daysInThisYear = totalTravelDays;
daysInNextYear = new TimeSpan(0);
}
if (dates.ContainsKey(currentYear))
dates[currentYear] += daysInThisYear.Value.Days;
else
dates[currentYear] = daysInThisYear.Value.Days;
if (dates.ContainsKey(nextYear))
dates[nextYear] += daysInNextYear.Value.Days;
else
dates[nextYear] = daysInNextYear.Value.Days;
}
Help appreciated :)
Assuming that var stays = List<Stay>();, you may try this:
var days = stays.SelectMany(s =>
Enumerable
.Range(0, (s.ToDate - s.FromDate).Days + 1)
.Select(d => s.FromDate.AddDays(d)))
.GroupBy(d => d.Year)
.Select(s => new { Year = s.Key, TotalDays = s.Count() })
.ToList();
days.ForEach(d =>
{
Console.WriteLine("{0} {1}", d.Year, d.TotalDays);
});
The output of the above is:
2001 32
2002 137
2003 60
2009 334
2010 32
2013 365
2014 365
2015 33
If you write a helper method to split date ranges into several ranges partitioned by year:
IEnumerable<Tuple<DateTime,DateTime>>
SplitDateRangeByYear(DateTime fromDate, DateTime toDate)
{
var start = fromDate;
for(var y = fromDate.Year; y < toDate.Year; ++y)
{
var nextYear = y + 1;
var nextYearStartDate = new DateTime(nextYear, 1, 1);
yield return Tuple.Create(start, nextYearStartDate);
start = nextYearStartDate;
}
yield return Tuple.Create(start, toDate);
}
Then you can write some handy Linq to do your bidding:
var yearlyTotals = stays
.SelectMany(s => SplitDateRangeByYear(s.FromDate, s.ToDate))
.GroupBy(x => x.Item1.Year)
.Select(g => new{
Year = g.Key,
NumDays= g.Sum(x => (x.Item2 - x.Item1).TotalDays)});
This is a more general solution that you requested because it will properly deal with sub-day TimeSpan components (i.e. your ranges include times of day).
You don't need a so big code to calculate the days between two dates; you can rely on a much simpler/more accurate approach. For example:
DateTime date1 = new DateTime(2013, 1, 1);
DateTime date2 = new DateTime(2015, 2, 2);
int totDays = Math.Abs(date1.Subtract(date2).Days);
Adapted to your specific situation:
int[] days = new int[Math.Abs(date1.Year - date2.Year) + 1];
int curYear = 0;
if (date1.Year != date2.Year)
{
if(date1.Year > date2.Year)
{
DateTime temp = date1;
date1 = date2;
date2 = temp;
}
int curYearNo = date1.Year - 1;
curYear = -1;
do
{
curYearNo = curYearNo + 1;
curYear = curYear + 1;
if (curYearNo < date2.Year)
{
days[curYear] = Math.Abs(new DateTime(curYearNo, 1, 1).Subtract(new DateTime(curYearNo, 12, 31)).Days) + 1; //Without +1 it would output 364/365 (because of not including both 1st January and 31st December)
}
else
{
days[curYear] = Math.Abs(new DateTime(curYearNo, 1, 1).Subtract(date2).Days);
}
} while (curYearNo < date2.Year);
}
else
{
days[curYear] = Math.Abs(date1.Subtract(date2).Days);
}
I am trying to get the affected hours between 2 datetime and all i found was a python solution.
For example 'start' is 09:30 and 'end' is 14:00 (same day). The values I'd like returned are
[9:00, 10:00, 11:00, 12:00, 13:00, 14:00]
Python get whole hour values between 2 datetime objects
I can't seem to find any equivalent to C#.
So you want a list of all hours between both dates? You can use this query:
TimeSpan ts = dt2 - dt1;
IEnumerable<int> hoursBetween = Enumerable.Range(0, (int)ts.TotalHours)
.Select(i => dt1.AddHours(i).Hour);
Sample dates:
DateTime dt1 = new DateTime(2013, 07, 08, 15, 50, 00);
DateTime dt2 = new DateTime(2013, 07, 10, 19, 30, 00);
TimeSpan ts = dt2 - dt1;
IEnumerable<int> hoursBetween = Enumerable.Range(0, (int)ts.TotalHours)
.Select(i => dt1.AddHours(i).Hour);
foreach (int hour in hoursBetween)
Console.WriteLine(hour);
Output:
15
16
17
18
19
20
21
22
23
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
The following will return the total hours between the 2 DateTime objects:
(datevalue1 - datevalue2).TotalHours
And for a custom behavior,such as displaying a list of hours, use a simple custom method on that Timespan created to get a list of hours in your desired format.
Code suggestion of the top of my head:
public List<String> GenerateHours(DateTime t1,DateTime t2){
if ((t2-t1).TotalHours >24)){
//decide what to do.
return null;
}else{
var currentHour = t2.Hour;
var list = new List<String>();
for (int i=0;i<(t2-t1).TotalHours;i++){
if (currentHour<10){
list.Add("0"+currentHour+":00");
}else if (currentHour>=10){
list.Add(currentHour+":00");
}
currentHour= (currentHour+1)%24;
}
return list;
}
}
public IEnumerable<DateTime> GetHoursBetween(DateTime start, DateTime end)
{
DateTime first = start.Date.AddHours(start.Hour);
for (DateTime dateTime = first; dateTime <= end; dateTime = dateTime.AddHours(1))
{
yield return dateTime;
}
}
TimeSpan ts = DateTime1 - DateTime2;
double totalHours = ts.TotalHours;
From MSDN: "Gets the value of the current TimeSpan structure expressed in whole and fractional hours."
EDIT: ok, now I see what you're asking for. How about this:
var d1 = DateTime.Today.AddHours(9.5);
var d2 = DateTime.Today.AddHours(14);
var first = new DateTime(d1.Year, d1.Month, d1.Day, d1.Minute == 0 ? d1.Hour : d1.Hour + 1, 0, 0);
var second = new DateTime(d2.Year, d2.Month, d2.Day, d2.Minute == 0 ? d2.Hour : d2.Hour + 1, 0, 0);
TimeSpan ts = second - first;
//returns DateTimes affected. I.e., Today at, [10:00, 11:00, 12:00, 13:00, 14:00]
IEnumerable<DateTime> dates = Enumerable.Range(0, (int)ts.TotalHours + 1).Select(hour => first.AddHours(hour));
//Or, if you just want the HOURs
//returns just ints: i.e., DateTimes 10,11,12,13,14
IEnumerable<int> hours = Enumerable.Range(0, (int)ts.TotalHours + 1).Select(hour => first.AddHours(hour).Hour);
The first method is needed if you actually have dates that span days. If you DON'T, then the second method that just returns the hours would work fine.
This should do the trick. Tested in LinqPad.
var startDate = new DateTime(2013, 8, 7, 9, 30, 0);
var endDate = new DateTime(2013, 8, 7, 14, 0, 0);
List<string> times = new List<string>();
var currentTime = startDate;
if (currentTime.Minute != 0 || currentTime.Second != 0) {
// Get next hour
currentTime = currentTime.AddHours(1).AddMinutes(currentTime.Minute * -1);
}
while (currentTime <= endDate) {
times.Add(string.Format("{0:00}:00", currentTime.Hour));
currentTime = currentTime.AddHours(1);
}
(int)Math.Abs((date1 - date2).TotalHours)
Simply subtract them and get the total of hours from the result. Something like this:
var totalHours = (dateTime1 - dateTime2).TotalHours;
Maybe something like this would work?
public static List<DateTime> GetAffectedHours(DateTime start, DateTime end)
{
List<DateTime> result = new List<DateTime>();
// Strip start of its minutes/seconds/etc
DateTime initial = new DateTime(start.Year, start.Month, start.Day, start.Hour, 0, 0);
// Go ahead and get the next hour
DateTime iterator = initial.AddHours(1.0);
// if it is still before the end
while (iterator < end)
{
// add it to the results list
result.Add(iterator);
// and get the next hour
iterator = iterator.AddHours(1.0);
}
return result;
}
You can use a For loop
List<int> allHoursBetween = new List<int>();
for (DateTime d = myStartDate; d <= myEndDate; d = d.AddHours(1))
allHoursBetween.Add(d.Hour);
I want to count data of each day from database using the for loop. Here, I don't know to get the begining of day (start from 12 am) and end of that day ( 12 pm) from value of only date. In below code startDate and endDate have only date value e.g. 2/11/2012.
for (DateTime dates = startDate; dates <= endDate; dates.AddDays(1))
{
DateTime BeginingOfDay = begining of value variable dates; // 2/2/2012 00:00:00
DateTime EndOfDay = at end of value variable dates; // 2/2/2012 23:59:59
int count = (from u in db.CDRs where (u.StartTime >= BeginingOfDay && u.StartTime <= EndOfDay) select u).Count();;
dictionary.Add(dates.ToString("MM/dd/yyyy"), count);
}
The best way to deal with this is to use the right combination of lessthan/greaterthan operators with midnight on day n, and midnight on day n+1
so given a day, eg
var date = new Date(2012,8,24); // today
get midnight on that day (start of the day)
var start = new Date(date.Year, date.Month, date.Day, 0,0,0); // could also be date.Date
and to get midnight on the next day just add 1 day
var end = start.AddDays(1);
now use greater-than-or-equal-to for the start, and less-than for the end:
var inRange = x.StartTime>=start && x.EndTime<end
Put together into your example becomes:
for (DateTime dates = startDate; dates <= endDate; dates.AddDays(1))
{
DateTime BeginingOfDay = new DateTime(dates.Year,dates.Month,dates.Day,0,0,0);
DateTime EndOfDay = BeginingOfDay.AddDays(1);
int count = (from u in db.CDRs where (u.StartTime >= BeginingOfDay && u.StartTime < EndOfDay) select u).Count();;
dictionary.Add(dates.ToString("MM/dd/yyyy"), count);
}
This should get you the results you want:
using(var dataContext = new YourDataContext())
{
var dictionary = dataContext.CDRs.GroupBy(u => new
{
u.StartTime.Year,
u.StartTime.Month,
u.StartTime.Day
})
.Select(g => new{ Date = g.Key, Count = g.Count() })
.ToDictionary(g => new DateTime(g.Key.Year, g.Key.Month, g.Key.Day), g=>g.Count);
return dictionary;
}