Calculating the number of days a range of dates c#










-2














Updated



I have a list of travels in the database with a various start and end dates, i would like to calculate the sum number of days that each travel has that corresponds with the defined range.



For example:



I want to query the number of days for the trip to Boston in the range following range:



RangeEnd is 27. Nov. 2017
RangeStart is 1. Nov. 2017



Here is my updated code:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var Trip = travel.Where(t => t.Country=="Boston");
var sumOfDays= Trip.Sum(t => ????);


The expected result for this case is 20 days.
Can anyone help?










share|improve this question























  • So does travel12TimesCount contain the right number? If so, why not?
    – DavidG
    Nov 8 at 14:57






  • 5




    It's unclear what you are asking. What do you expect the output to be, and what are you getting?
    – Jason Armstrong
    Nov 8 at 14:58






  • 3




    Sum(s => m.EndOfMonth what is that m?
    – fubo
    Nov 8 at 14:58











  • If you create two DateTime objects and subtract them, you will get a TimeSpan. You can read the number of days from that object. You can also get the number of hours (for rounding).
    – Flydog57
    Nov 8 at 15:02






  • 1




    The expected output from the example is 26 - here you go: dotnetfiddle.net/b3LOzn
    – fubo
    Nov 8 at 15:04
















-2














Updated



I have a list of travels in the database with a various start and end dates, i would like to calculate the sum number of days that each travel has that corresponds with the defined range.



For example:



I want to query the number of days for the trip to Boston in the range following range:



RangeEnd is 27. Nov. 2017
RangeStart is 1. Nov. 2017



Here is my updated code:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var Trip = travel.Where(t => t.Country=="Boston");
var sumOfDays= Trip.Sum(t => ????);


The expected result for this case is 20 days.
Can anyone help?










share|improve this question























  • So does travel12TimesCount contain the right number? If so, why not?
    – DavidG
    Nov 8 at 14:57






  • 5




    It's unclear what you are asking. What do you expect the output to be, and what are you getting?
    – Jason Armstrong
    Nov 8 at 14:58






  • 3




    Sum(s => m.EndOfMonth what is that m?
    – fubo
    Nov 8 at 14:58











  • If you create two DateTime objects and subtract them, you will get a TimeSpan. You can read the number of days from that object. You can also get the number of hours (for rounding).
    – Flydog57
    Nov 8 at 15:02






  • 1




    The expected output from the example is 26 - here you go: dotnetfiddle.net/b3LOzn
    – fubo
    Nov 8 at 15:04














-2












-2








-2







Updated



I have a list of travels in the database with a various start and end dates, i would like to calculate the sum number of days that each travel has that corresponds with the defined range.



For example:



I want to query the number of days for the trip to Boston in the range following range:



RangeEnd is 27. Nov. 2017
RangeStart is 1. Nov. 2017



Here is my updated code:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var Trip = travel.Where(t => t.Country=="Boston");
var sumOfDays= Trip.Sum(t => ????);


The expected result for this case is 20 days.
Can anyone help?










share|improve this question















Updated



I have a list of travels in the database with a various start and end dates, i would like to calculate the sum number of days that each travel has that corresponds with the defined range.



For example:



I want to query the number of days for the trip to Boston in the range following range:



RangeEnd is 27. Nov. 2017
RangeStart is 1. Nov. 2017



Here is my updated code:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var Trip = travel.Where(t => t.Country=="Boston");
var sumOfDays= Trip.Sum(t => ????);


The expected result for this case is 20 days.
Can anyone help?







c# ienumerable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 10:55

























asked Nov 8 at 14:54









Sam

11




11











  • So does travel12TimesCount contain the right number? If so, why not?
    – DavidG
    Nov 8 at 14:57






  • 5




    It's unclear what you are asking. What do you expect the output to be, and what are you getting?
    – Jason Armstrong
    Nov 8 at 14:58






  • 3




    Sum(s => m.EndOfMonth what is that m?
    – fubo
    Nov 8 at 14:58











  • If you create two DateTime objects and subtract them, you will get a TimeSpan. You can read the number of days from that object. You can also get the number of hours (for rounding).
    – Flydog57
    Nov 8 at 15:02






  • 1




    The expected output from the example is 26 - here you go: dotnetfiddle.net/b3LOzn
    – fubo
    Nov 8 at 15:04

















  • So does travel12TimesCount contain the right number? If so, why not?
    – DavidG
    Nov 8 at 14:57






  • 5




    It's unclear what you are asking. What do you expect the output to be, and what are you getting?
    – Jason Armstrong
    Nov 8 at 14:58






  • 3




    Sum(s => m.EndOfMonth what is that m?
    – fubo
    Nov 8 at 14:58











  • If you create two DateTime objects and subtract them, you will get a TimeSpan. You can read the number of days from that object. You can also get the number of hours (for rounding).
    – Flydog57
    Nov 8 at 15:02






  • 1




    The expected output from the example is 26 - here you go: dotnetfiddle.net/b3LOzn
    – fubo
    Nov 8 at 15:04
















So does travel12TimesCount contain the right number? If so, why not?
– DavidG
Nov 8 at 14:57




So does travel12TimesCount contain the right number? If so, why not?
– DavidG
Nov 8 at 14:57




5




5




It's unclear what you are asking. What do you expect the output to be, and what are you getting?
– Jason Armstrong
Nov 8 at 14:58




It's unclear what you are asking. What do you expect the output to be, and what are you getting?
– Jason Armstrong
Nov 8 at 14:58




3




3




Sum(s => m.EndOfMonth what is that m?
– fubo
Nov 8 at 14:58





Sum(s => m.EndOfMonth what is that m?
– fubo
Nov 8 at 14:58













If you create two DateTime objects and subtract them, you will get a TimeSpan. You can read the number of days from that object. You can also get the number of hours (for rounding).
– Flydog57
Nov 8 at 15:02




If you create two DateTime objects and subtract them, you will get a TimeSpan. You can read the number of days from that object. You can also get the number of hours (for rounding).
– Flydog57
Nov 8 at 15:02




1




1




The expected output from the example is 26 - here you go: dotnetfiddle.net/b3LOzn
– fubo
Nov 8 at 15:04





The expected output from the example is 26 - here you go: dotnetfiddle.net/b3LOzn
– fubo
Nov 8 at 15:04













3 Answers
3






active

oldest

votes


















0














Thank you @Flydog57 for helping me figuring it out. i took your idea of calling another function and it worked with the range.



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);

var Trip= travel.Where(s => s != null && s.City == "Boston");

var sumOfDays= Trip.Sum(t => GetTravelDaysWithinRange(t.Start,t.End,rangeStart, rangeEnd));


And the function that calculates the days is:



 public int GetTravelDaysWithinRange( DateTime start, DateTime end, DateTime rangeStart, DateTime rangeEnd)

int days = 0;
while (start <= end)

if (start.Month>= rangeStart.Month && start.Month <= rangeEnd.Month && start.Year >= rangeStart.Year && start.Year <= rangeEnd.Year)

++days;

start = start.AddDays(1);

return days;



And the exact value of 20 was resulted :)))))






share|improve this answer




















  • By the way, the correct way to say "thank you" is to upvote any answer you find useful. If one of the answers is the one that you ended up using to get to a solution, you can accept it. That way, years from now, when someone googles looking for a solution to a problem similar to yours, they will find answers in a nice sort order.
    – Flydog57
    Nov 12 at 2:31










  • For what it's worth, my guess is that if you changed the last line of my function from return (endInRange - startInRange).Days; to return (endInRange - startInRange).Days + 1;, you'd get the same answer. And, it's not comparing the bits of a DateTime or iterating in a loop.
    – Flydog57
    Nov 14 at 0:45


















0














This can be done in 1 line



var Trip = travel.Where(t => t.Country == "Boston").Select(x => (x.EndDate - x.StartDate).TotalDays).FirstOrDefault();


You need first or default incase there are 0 results or more than 1 recuslt






share|improve this answer




















  • That didn't consider the range start and end dates.
    – Sam
    Nov 9 at 20:55


















-1














How about something like this. The total I get is 55, which is what I expected:



Class Travel:



public class Travel

public Travel(string where, DateTime start, DateTime end)

Where = where;
StartDate = start;
EndDate = end;

public string Where get; set;
public DateTime StartDate get; set;
public DateTime EndDate get; set;



and then consuming it:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 9, 1), new DateTime(2017, 10, 1) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 9, 20) ),

;
var sumOfTravelDays = travel.Sum(t => (t.EndDate - t.StartDate).Days);


What kind of issue are you seeing?



Update



Add this function to the Travel class (either in the class or as an extension method):



 public int GetTravelDaysWithinRange(DateTime rangeStart, DateTime rangeEnd)

//if everything is outside of the range to be considered, return 0
if (rangeEnd < StartDate


Now, when I run a variation of my original code, it does what I think you want:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var sumOfDates = travel.Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));





share|improve this answer






















  • Thanks for your input. The issue is that is not related to the variables: m.EndOfMonth is 27. Nov. 2017 m.StartOfMonth is 1. Nov. 2017 What i'm trying to achive is to get only the number of days that are in this range only.
    – Sam
    Nov 8 at 16:24










  • @Sam: I've updated my answer. I think it should meet your needs
    – Flydog57
    Nov 8 at 19:14










  • Not really :( In your example, lets say we want to get the number of days for the trips in Boston in the same date range which is Nobember 1 to 27 2017. Still I'm not able to do that...
    – Sam
    Nov 9 at 10:26










  • I'm not following. I get Nov 1 (since it started on October 10, but you are only consider dates between Nov 1 and Nov 27) to Nov 20. I subtract the two and get 19 days. Now, if I travel from Monday to Friday is that 5 days or 4 days - what does your spec say? My calculation comes up with 4 days (because it's simple subtraction). If you want 5 days, well, that's a simple off-by-one error and you get to fix that (hint: add one in GetTravelDaysWithinRange - you have a debugger and a compiler and should be able to fix it). Otherwise, this code passed all my tests.
    – Flydog57
    Nov 9 at 15:08











  • Oh, wait a minute, you updated your post (with my code, actually). If you want to extract the trip to Boston and get the days of travel for that particular trip (only in the range Nov1 to Nov27), use this: var sumOfDates = travel.Where(t=>t.Where == "Boston").Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));. The Where clause reduces the list to just a single entry, while the Sum clause comes up with the total travel days. And, there's the same possible off-by-one issue that you get to figure out
    – Flydog57
    Nov 9 at 15:14











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53210246%2fcalculating-the-number-of-days-a-range-of-dates-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Thank you @Flydog57 for helping me figuring it out. i took your idea of calling another function and it worked with the range.



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);

var Trip= travel.Where(s => s != null && s.City == "Boston");

var sumOfDays= Trip.Sum(t => GetTravelDaysWithinRange(t.Start,t.End,rangeStart, rangeEnd));


And the function that calculates the days is:



 public int GetTravelDaysWithinRange( DateTime start, DateTime end, DateTime rangeStart, DateTime rangeEnd)

int days = 0;
while (start <= end)

if (start.Month>= rangeStart.Month && start.Month <= rangeEnd.Month && start.Year >= rangeStart.Year && start.Year <= rangeEnd.Year)

++days;

start = start.AddDays(1);

return days;



And the exact value of 20 was resulted :)))))






share|improve this answer




















  • By the way, the correct way to say "thank you" is to upvote any answer you find useful. If one of the answers is the one that you ended up using to get to a solution, you can accept it. That way, years from now, when someone googles looking for a solution to a problem similar to yours, they will find answers in a nice sort order.
    – Flydog57
    Nov 12 at 2:31










  • For what it's worth, my guess is that if you changed the last line of my function from return (endInRange - startInRange).Days; to return (endInRange - startInRange).Days + 1;, you'd get the same answer. And, it's not comparing the bits of a DateTime or iterating in a loop.
    – Flydog57
    Nov 14 at 0:45















0














Thank you @Flydog57 for helping me figuring it out. i took your idea of calling another function and it worked with the range.



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);

var Trip= travel.Where(s => s != null && s.City == "Boston");

var sumOfDays= Trip.Sum(t => GetTravelDaysWithinRange(t.Start,t.End,rangeStart, rangeEnd));


And the function that calculates the days is:



 public int GetTravelDaysWithinRange( DateTime start, DateTime end, DateTime rangeStart, DateTime rangeEnd)

int days = 0;
while (start <= end)

if (start.Month>= rangeStart.Month && start.Month <= rangeEnd.Month && start.Year >= rangeStart.Year && start.Year <= rangeEnd.Year)

++days;

start = start.AddDays(1);

return days;



And the exact value of 20 was resulted :)))))






share|improve this answer




















  • By the way, the correct way to say "thank you" is to upvote any answer you find useful. If one of the answers is the one that you ended up using to get to a solution, you can accept it. That way, years from now, when someone googles looking for a solution to a problem similar to yours, they will find answers in a nice sort order.
    – Flydog57
    Nov 12 at 2:31










  • For what it's worth, my guess is that if you changed the last line of my function from return (endInRange - startInRange).Days; to return (endInRange - startInRange).Days + 1;, you'd get the same answer. And, it's not comparing the bits of a DateTime or iterating in a loop.
    – Flydog57
    Nov 14 at 0:45













0












0








0






Thank you @Flydog57 for helping me figuring it out. i took your idea of calling another function and it worked with the range.



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);

var Trip= travel.Where(s => s != null && s.City == "Boston");

var sumOfDays= Trip.Sum(t => GetTravelDaysWithinRange(t.Start,t.End,rangeStart, rangeEnd));


And the function that calculates the days is:



 public int GetTravelDaysWithinRange( DateTime start, DateTime end, DateTime rangeStart, DateTime rangeEnd)

int days = 0;
while (start <= end)

if (start.Month>= rangeStart.Month && start.Month <= rangeEnd.Month && start.Year >= rangeStart.Year && start.Year <= rangeEnd.Year)

++days;

start = start.AddDays(1);

return days;



And the exact value of 20 was resulted :)))))






share|improve this answer












Thank you @Flydog57 for helping me figuring it out. i took your idea of calling another function and it worked with the range.



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);

var Trip= travel.Where(s => s != null && s.City == "Boston");

var sumOfDays= Trip.Sum(t => GetTravelDaysWithinRange(t.Start,t.End,rangeStart, rangeEnd));


And the function that calculates the days is:



 public int GetTravelDaysWithinRange( DateTime start, DateTime end, DateTime rangeStart, DateTime rangeEnd)

int days = 0;
while (start <= end)

if (start.Month>= rangeStart.Month && start.Month <= rangeEnd.Month && start.Year >= rangeStart.Year && start.Year <= rangeEnd.Year)

++days;

start = start.AddDays(1);

return days;



And the exact value of 20 was resulted :)))))







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 19:23









Sam

11




11











  • By the way, the correct way to say "thank you" is to upvote any answer you find useful. If one of the answers is the one that you ended up using to get to a solution, you can accept it. That way, years from now, when someone googles looking for a solution to a problem similar to yours, they will find answers in a nice sort order.
    – Flydog57
    Nov 12 at 2:31










  • For what it's worth, my guess is that if you changed the last line of my function from return (endInRange - startInRange).Days; to return (endInRange - startInRange).Days + 1;, you'd get the same answer. And, it's not comparing the bits of a DateTime or iterating in a loop.
    – Flydog57
    Nov 14 at 0:45
















  • By the way, the correct way to say "thank you" is to upvote any answer you find useful. If one of the answers is the one that you ended up using to get to a solution, you can accept it. That way, years from now, when someone googles looking for a solution to a problem similar to yours, they will find answers in a nice sort order.
    – Flydog57
    Nov 12 at 2:31










  • For what it's worth, my guess is that if you changed the last line of my function from return (endInRange - startInRange).Days; to return (endInRange - startInRange).Days + 1;, you'd get the same answer. And, it's not comparing the bits of a DateTime or iterating in a loop.
    – Flydog57
    Nov 14 at 0:45















By the way, the correct way to say "thank you" is to upvote any answer you find useful. If one of the answers is the one that you ended up using to get to a solution, you can accept it. That way, years from now, when someone googles looking for a solution to a problem similar to yours, they will find answers in a nice sort order.
– Flydog57
Nov 12 at 2:31




By the way, the correct way to say "thank you" is to upvote any answer you find useful. If one of the answers is the one that you ended up using to get to a solution, you can accept it. That way, years from now, when someone googles looking for a solution to a problem similar to yours, they will find answers in a nice sort order.
– Flydog57
Nov 12 at 2:31












For what it's worth, my guess is that if you changed the last line of my function from return (endInRange - startInRange).Days; to return (endInRange - startInRange).Days + 1;, you'd get the same answer. And, it's not comparing the bits of a DateTime or iterating in a loop.
– Flydog57
Nov 14 at 0:45




For what it's worth, my guess is that if you changed the last line of my function from return (endInRange - startInRange).Days; to return (endInRange - startInRange).Days + 1;, you'd get the same answer. And, it's not comparing the bits of a DateTime or iterating in a loop.
– Flydog57
Nov 14 at 0:45













0














This can be done in 1 line



var Trip = travel.Where(t => t.Country == "Boston").Select(x => (x.EndDate - x.StartDate).TotalDays).FirstOrDefault();


You need first or default incase there are 0 results or more than 1 recuslt






share|improve this answer




















  • That didn't consider the range start and end dates.
    – Sam
    Nov 9 at 20:55















0














This can be done in 1 line



var Trip = travel.Where(t => t.Country == "Boston").Select(x => (x.EndDate - x.StartDate).TotalDays).FirstOrDefault();


You need first or default incase there are 0 results or more than 1 recuslt






share|improve this answer




















  • That didn't consider the range start and end dates.
    – Sam
    Nov 9 at 20:55













0












0








0






This can be done in 1 line



var Trip = travel.Where(t => t.Country == "Boston").Select(x => (x.EndDate - x.StartDate).TotalDays).FirstOrDefault();


You need first or default incase there are 0 results or more than 1 recuslt






share|improve this answer












This can be done in 1 line



var Trip = travel.Where(t => t.Country == "Boston").Select(x => (x.EndDate - x.StartDate).TotalDays).FirstOrDefault();


You need first or default incase there are 0 results or more than 1 recuslt







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 19:44









China Syndrome

577516




577516











  • That didn't consider the range start and end dates.
    – Sam
    Nov 9 at 20:55
















  • That didn't consider the range start and end dates.
    – Sam
    Nov 9 at 20:55















That didn't consider the range start and end dates.
– Sam
Nov 9 at 20:55




That didn't consider the range start and end dates.
– Sam
Nov 9 at 20:55











-1














How about something like this. The total I get is 55, which is what I expected:



Class Travel:



public class Travel

public Travel(string where, DateTime start, DateTime end)

Where = where;
StartDate = start;
EndDate = end;

public string Where get; set;
public DateTime StartDate get; set;
public DateTime EndDate get; set;



and then consuming it:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 9, 1), new DateTime(2017, 10, 1) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 9, 20) ),

;
var sumOfTravelDays = travel.Sum(t => (t.EndDate - t.StartDate).Days);


What kind of issue are you seeing?



Update



Add this function to the Travel class (either in the class or as an extension method):



 public int GetTravelDaysWithinRange(DateTime rangeStart, DateTime rangeEnd)

//if everything is outside of the range to be considered, return 0
if (rangeEnd < StartDate


Now, when I run a variation of my original code, it does what I think you want:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var sumOfDates = travel.Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));





share|improve this answer






















  • Thanks for your input. The issue is that is not related to the variables: m.EndOfMonth is 27. Nov. 2017 m.StartOfMonth is 1. Nov. 2017 What i'm trying to achive is to get only the number of days that are in this range only.
    – Sam
    Nov 8 at 16:24










  • @Sam: I've updated my answer. I think it should meet your needs
    – Flydog57
    Nov 8 at 19:14










  • Not really :( In your example, lets say we want to get the number of days for the trips in Boston in the same date range which is Nobember 1 to 27 2017. Still I'm not able to do that...
    – Sam
    Nov 9 at 10:26










  • I'm not following. I get Nov 1 (since it started on October 10, but you are only consider dates between Nov 1 and Nov 27) to Nov 20. I subtract the two and get 19 days. Now, if I travel from Monday to Friday is that 5 days or 4 days - what does your spec say? My calculation comes up with 4 days (because it's simple subtraction). If you want 5 days, well, that's a simple off-by-one error and you get to fix that (hint: add one in GetTravelDaysWithinRange - you have a debugger and a compiler and should be able to fix it). Otherwise, this code passed all my tests.
    – Flydog57
    Nov 9 at 15:08











  • Oh, wait a minute, you updated your post (with my code, actually). If you want to extract the trip to Boston and get the days of travel for that particular trip (only in the range Nov1 to Nov27), use this: var sumOfDates = travel.Where(t=>t.Where == "Boston").Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));. The Where clause reduces the list to just a single entry, while the Sum clause comes up with the total travel days. And, there's the same possible off-by-one issue that you get to figure out
    – Flydog57
    Nov 9 at 15:14
















-1














How about something like this. The total I get is 55, which is what I expected:



Class Travel:



public class Travel

public Travel(string where, DateTime start, DateTime end)

Where = where;
StartDate = start;
EndDate = end;

public string Where get; set;
public DateTime StartDate get; set;
public DateTime EndDate get; set;



and then consuming it:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 9, 1), new DateTime(2017, 10, 1) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 9, 20) ),

;
var sumOfTravelDays = travel.Sum(t => (t.EndDate - t.StartDate).Days);


What kind of issue are you seeing?



Update



Add this function to the Travel class (either in the class or as an extension method):



 public int GetTravelDaysWithinRange(DateTime rangeStart, DateTime rangeEnd)

//if everything is outside of the range to be considered, return 0
if (rangeEnd < StartDate


Now, when I run a variation of my original code, it does what I think you want:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var sumOfDates = travel.Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));





share|improve this answer






















  • Thanks for your input. The issue is that is not related to the variables: m.EndOfMonth is 27. Nov. 2017 m.StartOfMonth is 1. Nov. 2017 What i'm trying to achive is to get only the number of days that are in this range only.
    – Sam
    Nov 8 at 16:24










  • @Sam: I've updated my answer. I think it should meet your needs
    – Flydog57
    Nov 8 at 19:14










  • Not really :( In your example, lets say we want to get the number of days for the trips in Boston in the same date range which is Nobember 1 to 27 2017. Still I'm not able to do that...
    – Sam
    Nov 9 at 10:26










  • I'm not following. I get Nov 1 (since it started on October 10, but you are only consider dates between Nov 1 and Nov 27) to Nov 20. I subtract the two and get 19 days. Now, if I travel from Monday to Friday is that 5 days or 4 days - what does your spec say? My calculation comes up with 4 days (because it's simple subtraction). If you want 5 days, well, that's a simple off-by-one error and you get to fix that (hint: add one in GetTravelDaysWithinRange - you have a debugger and a compiler and should be able to fix it). Otherwise, this code passed all my tests.
    – Flydog57
    Nov 9 at 15:08











  • Oh, wait a minute, you updated your post (with my code, actually). If you want to extract the trip to Boston and get the days of travel for that particular trip (only in the range Nov1 to Nov27), use this: var sumOfDates = travel.Where(t=>t.Where == "Boston").Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));. The Where clause reduces the list to just a single entry, while the Sum clause comes up with the total travel days. And, there's the same possible off-by-one issue that you get to figure out
    – Flydog57
    Nov 9 at 15:14














-1












-1








-1






How about something like this. The total I get is 55, which is what I expected:



Class Travel:



public class Travel

public Travel(string where, DateTime start, DateTime end)

Where = where;
StartDate = start;
EndDate = end;

public string Where get; set;
public DateTime StartDate get; set;
public DateTime EndDate get; set;



and then consuming it:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 9, 1), new DateTime(2017, 10, 1) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 9, 20) ),

;
var sumOfTravelDays = travel.Sum(t => (t.EndDate - t.StartDate).Days);


What kind of issue are you seeing?



Update



Add this function to the Travel class (either in the class or as an extension method):



 public int GetTravelDaysWithinRange(DateTime rangeStart, DateTime rangeEnd)

//if everything is outside of the range to be considered, return 0
if (rangeEnd < StartDate


Now, when I run a variation of my original code, it does what I think you want:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var sumOfDates = travel.Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));





share|improve this answer














How about something like this. The total I get is 55, which is what I expected:



Class Travel:



public class Travel

public Travel(string where, DateTime start, DateTime end)

Where = where;
StartDate = start;
EndDate = end;

public string Where get; set;
public DateTime StartDate get; set;
public DateTime EndDate get; set;



and then consuming it:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 9, 1), new DateTime(2017, 10, 1) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 9, 20) ),

;
var sumOfTravelDays = travel.Sum(t => (t.EndDate - t.StartDate).Days);


What kind of issue are you seeing?



Update



Add this function to the Travel class (either in the class or as an extension method):



 public int GetTravelDaysWithinRange(DateTime rangeStart, DateTime rangeEnd)

//if everything is outside of the range to be considered, return 0
if (rangeEnd < StartDate


Now, when I run a variation of my original code, it does what I think you want:



 var travel = new List<Travel>

new Travel("Egypt", new DateTime(2017, 8, 4), new DateTime(2017, 8, 24) ),
new Travel("Spain", new DateTime(2017, 11, 1), new DateTime(2017, 12, 10) ),
new Travel("Detroit", new DateTime(2017,9, 15), new DateTime(2017, 12, 20) ),
new Travel("Boston", new DateTime(2017,10, 15), new DateTime(2017, 11, 20) ),

;
var rangeStart = new DateTime(2017, 11, 1);
var rangeEnd = new DateTime(2017, 11, 27);
var sumOfDates = travel.Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 at 0:49

























answered Nov 8 at 15:05









Flydog57

1,7462510




1,7462510











  • Thanks for your input. The issue is that is not related to the variables: m.EndOfMonth is 27. Nov. 2017 m.StartOfMonth is 1. Nov. 2017 What i'm trying to achive is to get only the number of days that are in this range only.
    – Sam
    Nov 8 at 16:24










  • @Sam: I've updated my answer. I think it should meet your needs
    – Flydog57
    Nov 8 at 19:14










  • Not really :( In your example, lets say we want to get the number of days for the trips in Boston in the same date range which is Nobember 1 to 27 2017. Still I'm not able to do that...
    – Sam
    Nov 9 at 10:26










  • I'm not following. I get Nov 1 (since it started on October 10, but you are only consider dates between Nov 1 and Nov 27) to Nov 20. I subtract the two and get 19 days. Now, if I travel from Monday to Friday is that 5 days or 4 days - what does your spec say? My calculation comes up with 4 days (because it's simple subtraction). If you want 5 days, well, that's a simple off-by-one error and you get to fix that (hint: add one in GetTravelDaysWithinRange - you have a debugger and a compiler and should be able to fix it). Otherwise, this code passed all my tests.
    – Flydog57
    Nov 9 at 15:08











  • Oh, wait a minute, you updated your post (with my code, actually). If you want to extract the trip to Boston and get the days of travel for that particular trip (only in the range Nov1 to Nov27), use this: var sumOfDates = travel.Where(t=>t.Where == "Boston").Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));. The Where clause reduces the list to just a single entry, while the Sum clause comes up with the total travel days. And, there's the same possible off-by-one issue that you get to figure out
    – Flydog57
    Nov 9 at 15:14

















  • Thanks for your input. The issue is that is not related to the variables: m.EndOfMonth is 27. Nov. 2017 m.StartOfMonth is 1. Nov. 2017 What i'm trying to achive is to get only the number of days that are in this range only.
    – Sam
    Nov 8 at 16:24










  • @Sam: I've updated my answer. I think it should meet your needs
    – Flydog57
    Nov 8 at 19:14










  • Not really :( In your example, lets say we want to get the number of days for the trips in Boston in the same date range which is Nobember 1 to 27 2017. Still I'm not able to do that...
    – Sam
    Nov 9 at 10:26










  • I'm not following. I get Nov 1 (since it started on October 10, but you are only consider dates between Nov 1 and Nov 27) to Nov 20. I subtract the two and get 19 days. Now, if I travel from Monday to Friday is that 5 days or 4 days - what does your spec say? My calculation comes up with 4 days (because it's simple subtraction). If you want 5 days, well, that's a simple off-by-one error and you get to fix that (hint: add one in GetTravelDaysWithinRange - you have a debugger and a compiler and should be able to fix it). Otherwise, this code passed all my tests.
    – Flydog57
    Nov 9 at 15:08











  • Oh, wait a minute, you updated your post (with my code, actually). If you want to extract the trip to Boston and get the days of travel for that particular trip (only in the range Nov1 to Nov27), use this: var sumOfDates = travel.Where(t=>t.Where == "Boston").Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));. The Where clause reduces the list to just a single entry, while the Sum clause comes up with the total travel days. And, there's the same possible off-by-one issue that you get to figure out
    – Flydog57
    Nov 9 at 15:14
















Thanks for your input. The issue is that is not related to the variables: m.EndOfMonth is 27. Nov. 2017 m.StartOfMonth is 1. Nov. 2017 What i'm trying to achive is to get only the number of days that are in this range only.
– Sam
Nov 8 at 16:24




Thanks for your input. The issue is that is not related to the variables: m.EndOfMonth is 27. Nov. 2017 m.StartOfMonth is 1. Nov. 2017 What i'm trying to achive is to get only the number of days that are in this range only.
– Sam
Nov 8 at 16:24












@Sam: I've updated my answer. I think it should meet your needs
– Flydog57
Nov 8 at 19:14




@Sam: I've updated my answer. I think it should meet your needs
– Flydog57
Nov 8 at 19:14












Not really :( In your example, lets say we want to get the number of days for the trips in Boston in the same date range which is Nobember 1 to 27 2017. Still I'm not able to do that...
– Sam
Nov 9 at 10:26




Not really :( In your example, lets say we want to get the number of days for the trips in Boston in the same date range which is Nobember 1 to 27 2017. Still I'm not able to do that...
– Sam
Nov 9 at 10:26












I'm not following. I get Nov 1 (since it started on October 10, but you are only consider dates between Nov 1 and Nov 27) to Nov 20. I subtract the two and get 19 days. Now, if I travel from Monday to Friday is that 5 days or 4 days - what does your spec say? My calculation comes up with 4 days (because it's simple subtraction). If you want 5 days, well, that's a simple off-by-one error and you get to fix that (hint: add one in GetTravelDaysWithinRange - you have a debugger and a compiler and should be able to fix it). Otherwise, this code passed all my tests.
– Flydog57
Nov 9 at 15:08





I'm not following. I get Nov 1 (since it started on October 10, but you are only consider dates between Nov 1 and Nov 27) to Nov 20. I subtract the two and get 19 days. Now, if I travel from Monday to Friday is that 5 days or 4 days - what does your spec say? My calculation comes up with 4 days (because it's simple subtraction). If you want 5 days, well, that's a simple off-by-one error and you get to fix that (hint: add one in GetTravelDaysWithinRange - you have a debugger and a compiler and should be able to fix it). Otherwise, this code passed all my tests.
– Flydog57
Nov 9 at 15:08













Oh, wait a minute, you updated your post (with my code, actually). If you want to extract the trip to Boston and get the days of travel for that particular trip (only in the range Nov1 to Nov27), use this: var sumOfDates = travel.Where(t=>t.Where == "Boston").Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));. The Where clause reduces the list to just a single entry, while the Sum clause comes up with the total travel days. And, there's the same possible off-by-one issue that you get to figure out
– Flydog57
Nov 9 at 15:14





Oh, wait a minute, you updated your post (with my code, actually). If you want to extract the trip to Boston and get the days of travel for that particular trip (only in the range Nov1 to Nov27), use this: var sumOfDates = travel.Where(t=>t.Where == "Boston").Sum(t => t.GetTravelDaysWithinRange(rangeStart, rangeEnd));. The Where clause reduces the list to just a single entry, while the Sum clause comes up with the total travel days. And, there's the same possible off-by-one issue that you get to figure out
– Flydog57
Nov 9 at 15:14


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53210246%2fcalculating-the-number-of-days-a-range-of-dates-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế