DateTime, DayOfWeek in range comparison
-
We solved a similar problem by using a mapping from DayOfWeek to integer that returned 7 instead of 0 for Sunday. This way your code would work, too. Not very nice but it works. For .net the week start on Sunday :(
-^-^-^-^-^-^-^- no risk no funk
This will fix the problem for Saturday-Sunday, but it will not solve for Saturday-Tuesday.
-
Hello, I'm stuck with the following issue: let's say there is a DateTime 'timepoint', wich is passed in my method. I need to check now, whether timepoint's DayOfWeek is between 'fromDayOfWeek' and 'toDayOfWeek'. that's what I've done so far:
protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
{
bool retValue;retValue = timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to; return retValue; }
This will work for ranges like Monday-Friday, Thursday-Wednesday, Wednesday-Wednesday, but it will fail for something like Saturday-Sunday, since Sunday (0) is lower than Saturday(6). Any idea on how to logically implement this issue? Thanks in advance.
why not:
protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
{
return timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to;
}:)
-Spacix All your skynet questions[^] belong to solved
I dislike the black-and-white voting system on questions/answers. X|
-
why not:
protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
{
return timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to;
}:)
-Spacix All your skynet questions[^] belong to solved
I dislike the black-and-white voting system on questions/answers. X|
because this doesn't work. testcase: timepoint = new DateTime(2008,5,4);//sunday, 0 from = DayOfWeek.Friday; // 5 to = DayOfWeek.Tuesday; // 2 return 0 >= 5 && 0 <= 2; return value is 'false', but sunday is between friday and tuesday!
-
Hello, I'm stuck with the following issue: let's say there is a DateTime 'timepoint', wich is passed in my method. I need to check now, whether timepoint's DayOfWeek is between 'fromDayOfWeek' and 'toDayOfWeek'. that's what I've done so far:
protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
{
bool retValue;retValue = timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to; return retValue; }
This will work for ranges like Monday-Friday, Thursday-Wednesday, Wednesday-Wednesday, but it will fail for something like Saturday-Sunday, since Sunday (0) is lower than Saturday(6). Any idea on how to logically implement this issue? Thanks in advance.
You can use the CultureInfo class to set the first day of the week.
using System.Threading;
Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
So, set the first day of the week to whatever day you want, and then do your comparison.
protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
{
Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek = from;
bool inRange = timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to;
Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
return inRange;
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Friday, May 2, 2008 1:26 PM
-
Hello, I'm stuck with the following issue: let's say there is a DateTime 'timepoint', wich is passed in my method. I need to check now, whether timepoint's DayOfWeek is between 'fromDayOfWeek' and 'toDayOfWeek'. that's what I've done so far:
protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
{
bool retValue;retValue = timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to; return retValue; }
This will work for ranges like Monday-Friday, Thursday-Wednesday, Wednesday-Wednesday, but it will fail for something like Saturday-Sunday, since Sunday (0) is lower than Saturday(6). Any idea on how to logically implement this issue? Thanks in advance.
return ( true ) ;
Every timepoint falls between some DayOfWeek X and some DayOfWeek Y. :-Dbuchstaben wrote:
Thursday-Wednesday
Did you mean that? If you want Sunday to sort higher than Saturday, then you can simply add seven to it, but you'll need to store the values as integers rather than as the provided enum values.
int x = (int) timepoint.DayOfWeek ;
int y = (int) from ;
int z = (int) to ;x += x==0 ? 7 : 0 ;
y += y==0 ? 7 : 0 ;
z += z==0 ? 7 : 0 ;return ( x >= y && x <= z ) ;
but that's rather ugly.
-
You can use the CultureInfo class to set the first day of the week.
using System.Threading;
Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
So, set the first day of the week to whatever day you want, and then do your comparison.
protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
{
Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek = from;
bool inRange = timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to;
Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
return inRange;
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Friday, May 2, 2008 1:26 PM
Huh, interesting. I wonder how it performs in a loop.
-
return ( true ) ;
Every timepoint falls between some DayOfWeek X and some DayOfWeek Y. :-Dbuchstaben wrote:
Thursday-Wednesday
Did you mean that? If you want Sunday to sort higher than Saturday, then you can simply add seven to it, but you'll need to store the values as integers rather than as the provided enum values.
int x = (int) timepoint.DayOfWeek ;
int y = (int) from ;
int z = (int) to ;x += x==0 ? 7 : 0 ;
y += y==0 ? 7 : 0 ;
z += z==0 ? 7 : 0 ;return ( x >= y && x <= z ) ;
but that's rather ugly.
You can set the first day of the week to be thursday, and then the comparison would work.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Huh, interesting. I wonder how it performs in a loop.
If performance is an issue (in a loop), you could always wait until the loop is done and reset to the proper 1st day at that point, thus saving n calls to do it in that checkpoint function.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hello, I'm stuck with the following issue: let's say there is a DateTime 'timepoint', wich is passed in my method. I need to check now, whether timepoint's DayOfWeek is between 'fromDayOfWeek' and 'toDayOfWeek'. that's what I've done so far:
protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
{
bool retValue;retValue = timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to; return retValue; }
This will work for ranges like Monday-Friday, Thursday-Wednesday, Wednesday-Wednesday, but it will fail for something like Saturday-Sunday, since Sunday (0) is lower than Saturday(6). Any idea on how to logically implement this issue? Thanks in advance.
Ignore my first post. This is the best way (notice no bizarre culture crap going on):
DayOfWeek from = DayOfWeek.Thursday;
DayOfWeek to = DayOfWeek.Tuesday;DayOfWeek currentDay = DayOfWeek.Wednesday;
bool inRange = (currentDay >= from || currentDay <= to);
Notice that I'm using
OR
instead ofAND
. If you makecurrentDay
Wednesday,inRange
will be false. If you make it any other day, it will be true. It's kind of pointless to do a Sunday-Saturday (or similar) comparison because any date will fall into the range. BTW, the comparison above also works for from=Saturday and to=Sunday."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Huh, interesting. I wonder how it performs in a loop.
Just thought I'd let you know I was completely wrong. :) I posted a new response to the OP with the correct solution.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Just thought I'd let you know I was completely wrong. :) I posted a new response to the OP with the correct solution.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001What? Say it ain't so! :omg:
-
Ignore my first post. This is the best way (notice no bizarre culture crap going on):
DayOfWeek from = DayOfWeek.Thursday;
DayOfWeek to = DayOfWeek.Tuesday;DayOfWeek currentDay = DayOfWeek.Wednesday;
bool inRange = (currentDay >= from || currentDay <= to);
Notice that I'm using
OR
instead ofAND
. If you makecurrentDay
Wednesday,inRange
will be false. If you make it any other day, it will be true. It's kind of pointless to do a Sunday-Saturday (or similar) comparison because any date will fall into the range. BTW, the comparison above also works for from=Saturday and to=Sunday."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001I was just reviewing a method I wrote a few months back where I was checking to see if a value (integer) is between two others. I decided I needed more than a true/false, there are (currently) four result values:
\[System.FlagsAttribute()\] public enum CheckRangeResult { WithinStrictRange = 0 , Low = 1 , High = 2 , WithinLooseRange = 3 }
Basically; given Min, Max, and Test we expect Min to be less than Max; and Test will be Lower than Min (1), Higher than Max (2), or WithinStrictRange (0). But, when Min is greater than Max and Test is between Min and Max, both tests are true, yielding WithinLooseRange (3). In the case presented here, when Min is greater than Max and Test is not between the values, that should translate to "true", but that's not currently supported. I think I need to add value 4 to indicate that Min is greater than Max, then WithinLooseRange would have value 7. I'll think about it over the weekend.
-
Ignore my first post. This is the best way (notice no bizarre culture crap going on):
DayOfWeek from = DayOfWeek.Thursday;
DayOfWeek to = DayOfWeek.Tuesday;DayOfWeek currentDay = DayOfWeek.Wednesday;
bool inRange = (currentDay >= from || currentDay <= to);
Notice that I'm using
OR
instead ofAND
. If you makecurrentDay
Wednesday,inRange
will be false. If you make it any other day, it will be true. It's kind of pointless to do a Sunday-Saturday (or similar) comparison because any date will fall into the range. BTW, the comparison above also works for from=Saturday and to=Sunday."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001It occurred to me late today that this might make a good Friday Programming Quiz. :-D
-
It occurred to me late today that this might make a good Friday Programming Quiz. :-D
Maybe even two questions... 1) Given the following:
DayOfWeek startDay = DayOfWeek.Thursday;
DayOfWeek endDay = DayOfWeek.Monday;
DayOfWeek currentDay = DayOfWeek.Wednesday;Write a C# publicly accessible function that determines whether or not the day falls between
startDay
andendDay
. The function should work for any combination ofstartDay
/endDay
values, and should return a boolean value (true if in range, false if not in range). Answer:public bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
{
return (currentDay >= startDay || currentDay <= endDay);
}- Given the function above, change the function so that the class encapsulating it does not have to be instantiated in order to use the function. Answer:
public static bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
{
return (currentDay >= startDay || currentDay <= endDay);
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Maybe even two questions... 1) Given the following:
DayOfWeek startDay = DayOfWeek.Thursday;
DayOfWeek endDay = DayOfWeek.Monday;
DayOfWeek currentDay = DayOfWeek.Wednesday;Write a C# publicly accessible function that determines whether or not the day falls between
startDay
andendDay
. The function should work for any combination ofstartDay
/endDay
values, and should return a boolean value (true if in range, false if not in range). Answer:public bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
{
return (currentDay >= startDay || currentDay <= endDay);
}- Given the function above, change the function so that the class encapsulating it does not have to be instantiated in order to use the function. Answer:
public static bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
{
return (currentDay >= startDay || currentDay <= endDay);
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001I don't think that works with the "normal" case where startDay < endDay. It's also not a very general solution (which is no crime). Given a number (value) line, two points on that line that define a range, and another point to test inclusion. Normal ........... 1 > 2 < 3 Inverted (endponts not in normal order) ........... 4 < 5 > 6 The test value could be in any of the spots labeled 1 - 6. We then need to assign true/false to these spots. Clearly 2 yields true, while 1 and 3 yield false. The question is about the inverted range; The OP needs to assign true to 4 and 6, false to 5. The code I wrote in November was designed to allow assigning true to 5, false to 4 and 6. I have now modified my helper method (and enum) so it returns one of six values (it was four previously). The application then needs to provide a method that handles the translation to true/false.
-
I don't think that works with the "normal" case where startDay < endDay. It's also not a very general solution (which is no crime). Given a number (value) line, two points on that line that define a range, and another point to test inclusion. Normal ........... 1 > 2 < 3 Inverted (endponts not in normal order) ........... 4 < 5 > 6 The test value could be in any of the spots labeled 1 - 6. We then need to assign true/false to these spots. Clearly 2 yields true, while 1 and 3 yield false. The question is about the inverted range; The OP needs to assign true to 4 and 6, false to 5. The code I wrote in November was designed to allow assigning true to 5, false to 4 and 6. I have now modified my helper method (and enum) so it returns one of six values (it was four previously). The application then needs to provide a method that handles the translation to true/false.
I think this should cover it then... :)
public static void DayInRange(DayOfWeek currentDay, DayOfWeek startDay, DayOfWeek endDay)
{
if (startDay == endDay)
{
return true;
}
if (startDay < endDay)
{
return (currentDay >= startDay && currentDay <= endDay);
}
return (currentDay >= startDay || currentDay <= endDay);
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
I think this should cover it then... :)
public static void DayInRange(DayOfWeek currentDay, DayOfWeek startDay, DayOfWeek endDay)
{
if (startDay == endDay)
{
return true;
}
if (startDay < endDay)
{
return (currentDay >= startDay && currentDay <= endDay);
}
return (currentDay >= startDay || currentDay <= endDay);
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001That hurts my eyes. (Not that I should talk. :-O )