c# enum flag check if string is in flag - c#

I have an enum:
[Flags]
public enum WeekDays
{
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64
}
And if I do:
var weekDays = WeekDays.Monday | WeekDays.Tuesday | WeekDays.Friday;
How can I check if a string for example Monday is set in the weekDays?

Check out the following code. You can use the HasFlag attribute:
class Program
{
[Flags]
public enum WeekDays
{
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64
}
private static string result;
static void Main()
{
var wd = new WeekDays();
Console.WriteLine(wd.HasFlag(WeekDays.Monday));
wd = WeekDays.Monday;
Console.WriteLine(wd.HasFlag(WeekDays.Monday));
bool is_defined = Enum.IsDefined(typeof(WeekDays), "Monday");
Console.WriteLine(is_defined);
bool is_not_defined = Enum.IsDefined(typeof(WeekDays), "Mondays");
Console.WriteLine(is_not_defined);
bool has_flag_by_string = Enum.TryParse<WeekDays>("Monday", out wd);
Console.WriteLine(has_flag_by_string);
Console.ReadLine();
}
}
EDIT
Added the ability to check if attribute exists by string.
EDIT 2
Added the Enum.TryParse<> to parse the string value.
Reference:
https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx

I think you want this
WeekDays days = WeekDays.Monday | WeekDays.Tuesday;
string monday = "Monday";
WeekDays day;
if (Enum.TryParse(monday, true, out day))
{
if (days.HasFlag(day))
{
Console.WriteLine("Has {0}", monday);
}
else
{
Console.WriteLine("Does not have {0}", monday);
}
}
else
{
Console.WriteLine("invalid string");
}
First you can use Enum.TryParse to parse the string to an enum value or determine if the string is invalid. Then use HasFlag to see if it is included.

Related

How to compare standard DayOfWeek to own with Flag

I have my own DaysOfWeek Flag enum (kind of https://learn.microsoft.com/en-us/previous-versions/ms886541(v=msdn.10))
[Flags]
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32
}
And I need to compare standard DayOfWeek with mine. How can I do that?
Since your enum uses the same order of days as the built-in DayOfWeek, all you need to do is to use the variable of DayOfWeek type as the exponent of 2 and then bitwise-AND it with your enum variable.
Something like this (this will check if Monday flag of your enum is 1 or not):
MyDayOfWeek Days = MyDayOfWeek.Monday | MyDayOfWeek.Friday;
DayOfWeek D = DayOfWeek.Monday;
var Mask = 1 << (int)D;
if ((Mask & (int)Days) == Mask)
//Do whatever;
I have renamed your enum to MyDaysOfWeek, whereas DayOfWeek is the built-in .NET type. You can do the same for any day of week.
Edit
As #HenkHolterman pointed out (thanks for that), you'll have problems with your Sunday set to 0. A Flags enum should normally start with a member named None that is equal to 0 and which indicates that none of the flags of the variable are set.
Thanks everyone for help.
Finally, I have solution
Own DaysOfWeek with flag:
[Flags]
public enum DaysOfWeek
{
None = 0,
Sunday = 1 << 0,
Monday = 1 << 1,
Tuesday = 1 << 2,
Wednesday = 1 << 3,
Thursday = 1 << 4,
Friday = 1 << 5,
Saturday = 1 << 6,
}
Since own enum has same order of days, we can write extension method to convert standard DayOfWeek to own
public static class EnumExtensions
{
public static DaysOfWeek ToFlag(this DayOfWeek dayOfWeek)
{
var mask = 1 << (int)dayOfWeek;
return (DaysOfWeek)Enum.ToObject(typeof(DaysOfWeek), mask);
}
}
And usage:
var days = DaysOfWeek.Sunday | DaysOfWeek.Friday;
var day = DayOfWeek.Sunday;
var ownDay = day.ToFlag();
if (days.HasFlag(ownDay))
// Do whatever;
playground: https://dotnetfiddle.net/sV3yge
public enum BitwiseDayOfWeek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
public class Program
{
public static void Main(string[] args)
{
BitwiseDayOfWeek scheduledDayOfWeek
= BitwiseDayOfWeek.Saturday | BitwiseDayOfWeek.Sunday;
// turn System.DayOfWeek (DateTime.Now.DayOfWeek) into BitwiseDayOfWeek
BitwiseDayOfWeek currentDayOfWeek
= (BitwiseDayOfWeek)Math.Pow(2, (double)DateTime.Now.DayOfWeek);
// test if today is the scheduled day
if ( (currentDayOfWeek & scheduledDayOfWeek) == currentDayOfWeek )
Console.WriteLine(currentDayOfWeek);
Console.WriteLine("---------------------");
Console.ReadLine();
}
}
If you change your Enum like this:
[Flags]
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
You can try this:
class Program
{
public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)
{
int mineInt = (int)mine;
int cSharpInt = (int)cSharp;
return mineInt == cSharpInt;
}
static void Main(string[] args)
{
DateTime dateTime = DateTime.Now;
DayOfWeek dayOfWeek = DayOfWeek.Sunday;
bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);
Console.WriteLine(areEqual);
Console.ReadKey();
}
}
If you can't change your Enum, you can try this:
class Program
{
public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)
{
if (mine == DayOfWeek.Friday && cSharp == System.DayOfWeek.Friday ||
mine == DayOfWeek.Monday && cSharp == System.DayOfWeek.Monday ||
mine == DayOfWeek.Saturday && cSharp == System.DayOfWeek.Saturday ||
mine == DayOfWeek.Sunday && cSharp == System.DayOfWeek.Sunday ||
mine == DayOfWeek.Thursday && cSharp == System.DayOfWeek.Thursday ||
mine == DayOfWeek.Tuesday && cSharp == System.DayOfWeek.Tuesday ||
mine == DayOfWeek.Wednesday && cSharp == System.DayOfWeek.Wednesday)
return true;
return false;
}
static void Main(string[] args)
{
DateTime dateTime = DateTime.Now;
DayOfWeek dayOfWeek = DayOfWeek.Tuesday;
bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);
Console.WriteLine(areEqual);
Console.ReadKey();
}
}

Howto get flags from enum value?

Trying to get array of all possible flags from enum value, say 3 to array of {1, 2}.
I have an extension
internal static MyEnum[] GetFlags(this MyEnum modKey)
{
string[] splitStr = modKey.ToString().Split(new string[1] { ", " }, StringSplitOptions.RemoveEmptyEntries);
MyEnum[] flags = new MyEnum[splitStr.Length];
for (int i = 0; i < splitStr.Length; i++)
{
flags[i] = (MyEnum)Enum.Parse(typeof(MyEnum), splitStr[i]);
}
return flags;
}
...but it seems a bit wasteful for the purpose. Could this be done more effectively?
You can simply filter all possible values of the MyEnum to the ones in modKey:
internal static MyEnum[] GetFlags(this MyEnum modKey)
{
return Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Where(v => modKey.HasFlag(v))
.ToArray();
}
Edit
Based on the comment below, in case of combinations specified, the method should only return the combinations, not all flags set.
The solution is to loop through all flags set in the enum starting from the highest one. In each iteration, we have to add a flag to the result, and remove it from the iterated enum until it's empty:
internal static MyEnum[] GetFlags(this MyEnum modKey)
{
List<MyEnum> result = new List<MyEnum>();
while (modKey != 0)
{
var highestFlag = Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.OrderByDescending(v => v)
.FirstOrDefault(v => modKey.HasFlag(v));
result.Add(highestFlag);
modKey ^= highestFlag;
}
return result.ToArray();
}
assuming your MyEnum has a Flags Attribute, to test if a flag is set the (standard?) way is to perform a binary & between your value and the flag you want to test:
so something like this should work:
internal static MyEnum[] GetFlags(this MyEnum modKey)
{
List<MyEnum> flags = new List<MyEnum>();
foreach (var flag in Enum.GetValues(typeof(MyEnum)))
{
if (modKey & flag == flag)
flags.Add((MyEnum)flag);
}
return flags.ToArray();
}
if you use .Net 4 or later, you can use HasFlag
if (modKey.HasFlag((MyEnum)flag))
...
Both answers don't do what (I think) is asked: get the elementary values from an enum value, not any composed values. One example where this may be useful is when one enum value must be used in a Contains statement in LINQ to a SQL backend that doesn't support HasFlag.
For this purpose I first created a method that returns elementary flags from an enum type:
public static class EnumUtil
{
public static IEnumerable<TEnum> GetFlags<TEnum>()
where TEnum : Enum
{
return Enum.GetValues(typeof(TEnum))
.Cast<TEnum>()
.Where(v =>
{
var x = Convert.ToInt64(v); // because enums can be Int64
return x != 0 && (x & (x - 1)) == 0;
// Checks whether x is a power of 2
// Example: when x = 16, the binary values are:
// x: 10000
// x-1: 01111
// x & (x-1): 00000
});
}
}
And then a method that returns elementary flags from an enum value:
public static IEnumerable<TEnum> GetFlags<TEnum>(this TEnum enumValue)
where TEnum : Enum
{
return GetFlags<TEnum>()
.Where(ev => enumValue.HasFlag(ev));
}
Usage
Taking this enum type:
[Flags]
public enum WeekDay
{
Monday = 1 << 0,
Tuesday = 1 << 1,
Wednesday = 1 << 2,
Thursday = 1 << 3,
Friday = 1 << 4,
Saturday = 1 << 5,
Sunday = 1 << 6,
BusinessDay = Monday | Tuesday | Wednesday | Thursday | Friday,
WeekendDay = Saturday | Sunday,
All = BusinessDay | WeekendDay
}
The statements (in Linqpad)...
string.Join(",", EnumUtil.GetFlags<WeekDay>()).Dump();
var t = WeekDay.Thursday | WeekDay.WeekendDay;
string.Join(",", t.GetFlags()).Dump();
t = WeekDay.All;
string.Join(",", t.GetFlags()).Dump();
...return this:
Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
Thursday,Saturday,Sunday
Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
Basic idea taken from this answer to my question on Code Review.

How to Get the number of weeks in a given year

Trying to code a correct function that returns the number of weeks in a given year, but without success.
Example of the function I'm looking for :
int weeks = GetWeeksInYear ( 2012 )
should return 52 weeks // means there are only 52 weeks in 2012.
P.s.: in a year can be 52, 53, 54 weeks, not sure about 51
See the Calendar.GetWeekOfYear method
public int GetWeeksInYear(int year)
{
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
DateTime date1 = new DateTime(year, 12, 31);
Calendar cal = dfi.Calendar;
return cal.GetWeekOfYear(date1, dfi.CalendarWeekRule,
dfi.FirstDayOfWeek);
}
Be carefull to figure out the correct CalendarWeekRule and FirstDayOfWeek for a Calendar that matches the culture your customers are used to. (for some calenders it might vary...)
Update 14 Oct 2019
If you're using .NET Core 3.0 and you want to get the number of weeks in a year conforming to ISO 8601 - you can use ISOWeek's GetWeeksInYear method.
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
Console.WriteLine(ISOWeek.GetWeeksInYear(2009)); // returns 53
}
}
Working example: https://dotnetfiddle.net/EpIbZQ
I had the issue when I assign 2018 to year parameter of other methods, so I extended the Code of Tim Schmelter. I do not know, maybe there are codes that work faster:
//you can try like that
int weeks = DateHelper.GetWeeksInGivenYear(2018);
int weeks = DateHelper.GetWeeksInGivenYear(2020);
// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(this DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
//gets given year last week no
public static int GetWeeksInGivenYear(int year)
{
DateTime lastDate = new DateTime(year, 12, 31);
int lastWeek = GetIso8601WeekOfYear(lastDate);
while (lastWeek == 1)
{
lastDate = lastDate.AddDays(-1);
lastWeek = GetIso8601WeekOfYear(lastDate);
}
return lastWeek;
}
P.s.: in a year can be 52, 53, 54 weeks, not sure about 51
1. If we will check With Calendar we will get results that we can have only 53 or 54 weeks.
2. This is incorrect result (read the end of my answer)
You can check it with the following app:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
namespace TestConsoleApp
{
class Program
{
public static void Main(string[] args)
{
var b = CountWeeksForYearsRange(1, 4000);
var c = b.Where(a => a.Value != 53).ToDictionary(a=>a.Key, a=>a.Value);
}
static DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
static Calendar calendar = DateTimeFormatInfo.CurrentInfo.Calendar;
private static int CountWeeksInYear(int year)
{
DateTime date = new DateTime(year, 12, 31);
return calendar.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
}
private static Dictionary<int,int> CountWeeksForYearsRange(int yearStart, int yearEnd)
{
Dictionary<int, int> rez = new Dictionary<int, int>();
for (int i = yearStart; i <= yearEnd; i++)
{
rez.Add(i, CountWeeksInYear(i));
}
return rez;
}
}
}
and there will be only 53 and 54 values.
This means that for faster work of method we can have pre-coded function for such situation.
Years with not 53 weeks
Years with not 53 and not 54 weeks:
So in this case we can generate simple array of years that have 54 weeks from 0 to 4000 years:
12,40,68,96,108,136,164,192,204,232,260,288,328,356,384,412,440,468,496,508,536,564,592,604,632,660,688,728,756,784,812,840,868,896,908,936,964,992,1004,1032,1060,1088,1128,1156,1184,1212,1240,1268,1296,1308,1336,1364,1392,1404,1432,1460,1488,1528,1556,1584,1612,1640,1668,1696,1708,1736,1764,1792,1804,1832,1860,1888,1928,1956,1984,2012,2040,2068,2096,2108,2136,2164,2192,2204,2232,2260,2288,2328,2356,2384,2412,2440,2468,2496,2508,2536,2564,2592,2604,2632,2660,2688,2728,2756,2784,2812,2840,2868,2896,2908,2936,2964,2992,3004,3032,3060,3088,3128,3156,3184,3212,3240,3268,3296,3308,3336,3364,3392,3404,3432,3460,3488,3528,3556,3584,3612,3640,3668,3696,3708,3736,3764,3792,3804,3832,3860,3888,3928,3956,3984
And this is means that most optimized method will be:
//Works only with 1-4000 years range
public int CountWeeksInYearOptimized(int year)
{
return (_yearsWith54Weeks.IndexOf(year) == -1) ? 53 : 54;
}
private List<int> _yearsWith54Weeks = new List<int> { 12, 40, 68, 96, 108, 136, 164, 192,
204, 232, 260, 288, 328, 356, 384, 412, 440, 468, 496, 508, 536, 564,
592, 604, 632, 660, 688, 728, 756, 784, 812, 840, 868, 896, 908, 936,
964, 992, 1004, 1032, 1060, 1088, 1128, 1156, 1184, 1212, 1240, 1268,
1296, 1308, 1336, 1364, 1392, 1404, 1432, 1460, 1488, 1528, 1556, 1584,
1612, 1640, 1668, 1696, 1708, 1736, 1764, 1792, 1804, 1832, 1860, 1888,
1928, 1956, 1984, 2012, 2040, 2068, 2096, 2108, 2136, 2164, 2192, 2204,
2468, 2496, 2508, 2536, 2564, 2592, 2604, 2632, 2660, 2688, 2728, 2756,
2784, 2812, 2840, 2868, 2896, 2908, 2936, 2964, 2992, 3004, 3032, 3060,
3088, 3128, 3156, 3184, 3212, 3240, 3268, 3296, 3308, 3336, 3364, 3392,
3668, 3696, 3708, 3736, 3764, 3792, 3804, 3832, 3860, 3888, 3928, 3956,
3984 };
or if you don't want to pre-calculated data:
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar calendar = DateTimeFormatInfo.CurrentInfo.Calendar;
private int CountWeeksInYear(int year)
{
DateTime date = new DateTime(year, 12, 31);
return calendar.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
}
UPD: BUT! Looks like this is incorrect way.
I don't know why so, but looks like Calendar saying incorrect number. And correct count of weeks is always on less on 1 week. You can check it manually:
Let's calc days in the following years with Calendar:
2011+2012+2013:
53+54+53=160 weeks.
But stop!
(365+366+365)/7 = 157.
So best way will be to do -1 to the value that will be shown by calendar
Or to use the following fastest method:
//Works only with 1-4000 years range
public int CountWeeksInYearOptimized(int year)
{
return (_yearsWith54Weeks.IndexOf(year) == -1) ? 52 : 53;
}
private List<int> _yearsWith54Weeks = new List<int> { 12, 40, 68, 96, 108, 136, 164, 192,
204, 232, 260, 288, 328, 356, 384, 412, 440, 468, 496, 508, 536, 564,
592, 604, 632, 660, 688, 728, 756, 784, 812, 840, 868, 896, 908, 936,
964, 992, 1004, 1032, 1060, 1088, 1128, 1156, 1184, 1212, 1240, 1268,
1296, 1308, 1336, 1364, 1392, 1404, 1432, 1460, 1488, 1528, 1556, 1584,
1612, 1640, 1668, 1696, 1708, 1736, 1764, 1792, 1804, 1832, 1860, 1888,
1928, 1956, 1984, 2012, 2040, 2068, 2096, 2108, 2136, 2164, 2192, 2204,
2468, 2496, 2508, 2536, 2564, 2592, 2604, 2632, 2660, 2688, 2728, 2756,
2784, 2812, 2840, 2868, 2896, 2908, 2936, 2964, 2992, 3004, 3032, 3060,
3088, 3128, 3156, 3184, 3212, 3240, 3268, 3296, 3308, 3336, 3364, 3392,
3668, 3696, 3708, 3736, 3764, 3792, 3804, 3832, 3860, 3888, 3928, 3956,
3984 };
//For ISO Calender(First Day Of Week : Monday)
DateTime start = new DateTime(2020,1,1);
int total_weeks=0;
//If a year starts on thursday or a leap year starts on wednesday then the year has 53 weeks
if((DateTime.IsLeapYear(start.Year) && start.ToString("dddd")=="Wednesday")||(start.ToString("dddd")=="Thursday"))
{
total_weeks=53;
}
else
{
total_week=52;
}
Console.WriteLine(total_weeks.ToString());

Get values from enum

I need find in enum DaysOfTheWeek values filtered by List<int> daysId and return filtered enum
My enum:
[Flags]
public enum DaysOfTheWeek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
AllDays = 127,
}
-
var allValues = Enum.GetValues(typeof(DaysOfTheWeek));
My integer list:
List<int> daysId = new List<int> {1,3};
var filtered = daysId.Select(d => (DaysOfTheWeek)d);
Not sure about your question. Something like this?
var filtered = daysId.Select(d=>(DaysOfTheWeek)(1<<(d-1))).ToList();
Maybe i'm over complicating, but check this out:
int[] daysId = new int[] { 1, 3,48,127 };
Dictionary<int, List<DaysOfTheWeek>> result = new Dictionary<int, List<DaysOfTheWeek>>();
foreach (int id in daysId)
{
result[id] = new List<DaysOfTheWeek>();
BitArray ba = new BitArray(new int[]{id});
for (int i=0;i<ba.Length;i++)
{
if (ba[i])
{
result[id].Add((DaysOfTheWeek)(1 << i));
Console.WriteLine((DaysOfTheWeek)(1 << i));
}
}
Console.WriteLine("id-" + id + " > " + (DaysOfTheWeek)id);
}
Console.ReadKey();
Check the output and see if it's what you want. You can use the direct conversion to days of week if you want so (DaysOfTheWeek)id or you can get each DaysOfTheWeek separately. I've done both up above.

How to check if a DateTime range is within another 3 month DateTime range

Hi I have a Start Date and End Date per record in a db.
I need to check to see where the time period falls in a 2 year period broken into two lots of quarters then display what quarters each record falls into.
Quarter 1 includes June 09, Jul 09, Aug 09
Quarter 2 includes Sept 09, Oct 09, Nov 09
Quarter 3 includes Dec 09, Jan 10, Feb 10
Quarter 4 includes Mar 10, Apr 10, May 10
Quaretr 5 includes Jun 10, Jul 10...
e.g. 01/10/09 - 01/06/10 would fall into quarters 2, 3, 4 & 5
I am very new to .NET so any examples would be much appreciated.
This should work for you also.
class Range
{
public DateTime Begin { get; private set; }
public DateTime End { get; private set; }
public Range(DateTime begin, DateTime end)
{
Begin = begin;
End = end;
}
public bool Contains(Range range)
{
return range.Begin >= Begin && range.End <= End;
}
}
and then to use it
List<Range> ranges = new List<Range>();
ranges.Add(new Range(DateTime.Now, DateTime.Now.AddMonths(3)));
ranges.Add(new Range(DateTime.Now.AddMonths(3), DateTime.Now.AddMonths(6)));
Range test = new Range(DateTime.Now.AddMonths(1), DateTime.Now.AddMonths(2));
var hits = ranges.Where(range => range.Contains(test));
MessageBox.Show(hits.Count().ToString());
You would call IntervalInQuarters as follows:
IntervalInQuarters(new DateTime(2007, 10, 10), new DateTime(2009, 10, 11));
The function returns a list of quarter start dates. Note that the range of quarters searched is defined within the function itself. Please edit as appropriate for your situation. They key point is to make sure the interval/quarter intersection logic is right.
private List<DateTime> IntervalInQuarters(DateTime myStartDate, DateTime myEndDate)
{
DateTime quarterStart = new DateTime(2006, 06, 01);
DateTime nextQuarterStart = new DateTime(2006, 09, 01);
DateTime finalDate = new DateTime(2011, 01, 01);
List<DateTime> foundQuarters = new List<DateTime>();
while (quarterStart < finalDate)
{
// quarter intersects interval if:
// its start/end date is within our interval
// our start/end date is within quarter interval
DateTime quarterEnd = nextQuarterStart.AddDays(-1);
if (DateInInterval(myStartDate, quarterStart, quarterEnd) ||
DateInInterval(myEndDate, quarterStart, quarterEnd) ||
DateInInterval(quarterStart, myStartDate, myEndDate) ||
DateInInterval(quarterEnd, myStartDate, myEndDate))
{
foundQuarters.Add(quarterStart);
}
quarterStart = nextQuarterStart;
nextQuarterStart = nextQuarterStart.AddMonths(3);
}
return foundQuarters;
}
private bool DateInInterval(DateTime myDate, DateTime intStart, DateTime intEnd)
{
return ((intStart <= myDate) && (myDate <= intEnd));
}
private void Form1_Load(object sender, EventArgs e)
{
DateTime[,] ranges = new DateTime[3,2];
//Range 1 - Jan to March
ranges[0, 0] = new DateTime(2010, 1, 1);
ranges[0, 1] = new DateTime(2010, 3, 1);
//Range 2 - April to July
ranges[1, 0] = new DateTime(2010, 4, 1);
ranges[1, 1] = new DateTime(2010, 7, 1);
//Range 3 - March to June
ranges[2, 0] = new DateTime(2010, 3, 1);
ranges[2, 1] = new DateTime(2010, 6, 1);
DateTime checkDate = new DateTime(2010, 4, 1);
string validRanges = string.Empty;
for (int i = 0; i < ranges.GetLength(0); i++)
{
if (DateWithin(ranges[i,0], ranges[i,1], checkDate))
{
validRanges += i.ToString() + " ";
}
}
MessageBox.Show(validRanges);
}
private bool DateWithin(DateTime dateStart, DateTime dateEnd, DateTime checkDate)
{
if (checkDate.CompareTo(dateStart) < 0 || checkDate.CompareTo(dateEnd) > 0)
{
return false;
}
return true;
}
You may have to take a look at: http://msdn.microsoft.com/en-us/library/03ybds8y(v=VS.100).aspx
This may start you up
FindQuarter(DateTime startDate, DateTime endDate) // 01-10-09, 01-06-10
{
startDateQuarter = GetQuarter(startDate.Month); // 2
endDateQuarter = GetQuarter(endDate.Month); // 1
endDateQuarter += (endDate.Year - startDate.Year) * 4; // 5
// fill up startDateQuarter to endDateQuarter into a list
// and return it // 2,3,4,5
}
GetQuarter(int month) // 6
{
int quarter;
// check the month value and accordingly assign one of the basic quarters
// using if-else construct ie, if(month>=6 && month<=8){ quarter = 1 };
return quarter; // 1
}
Instead of GetQuarter() method, you can also use a dictionary to store your month to quarter mappings
Dictionary<int, int> quarter = new Dictionary<int, int>();
quarter.Add(1,1); //of the format Add(month,quarter)
quarter.Add(2,1);
...
Now instead of GetQuarter(someDate.Month); you can use quarter[someDate.Month];
If you want to compare two dates you should find out the first day of the quarter corresponds every of this dates, then you can compare this two dates:
using System;
namespace DataTime {
class Program {
static int GetQuarter (DateTime dt) {
int Month = dt.Month; // from 1 to 12
return Month / 3 + 1;
}
static DateTime GetQuarterFirstDay (DateTime dt) {
int monthsOfTheFirstDayOfQuarter = (GetQuarter (dt) - 1) * 3 + 1;
return new DateTime(dt.Year, monthsOfTheFirstDayOfQuarter, 1);
// it can be changed to
// return new DateTime(dt.Year, (dt.Month/3)*3 + 1, 1);
}
static void Main (string[] args) {
DateTime dt1 = new DateTime (2009, 6, 9),
dt2 = new DateTime (2009, 7, 9),
dt3 = new DateTime (2009, 8, 9),
dt4 = new DateTime (2009, 8, 9);
Console.WriteLine ("dt1={0}", dt1.AddMonths (1));
Console.WriteLine ("dt2={0}", dt2.AddMonths (1));
Console.WriteLine ("dt3={0}", dt3.AddMonths (1));
DateTime startDate = DateTime.Now,
endDate1 = startDate.AddMonths(24).AddDays(1),
endDate2 = startDate.AddMonths(24).AddDays(-1),
endDate3 = startDate.AddMonths(28);
Console.WriteLine ("Now we have={0}", startDate);
Console.WriteLine ("endDate1={0}", endDate1);
Console.WriteLine ("endDate2={0}", endDate2);
Console.WriteLine ("endDate3={0}", endDate3);
Console.WriteLine ("GetQuarterFirstDay(startDate)={0}", GetQuarterFirstDay (startDate));
Console.WriteLine ("GetQuarterFirstDay(endDate1)={0}", GetQuarterFirstDay (endDate1));
Console.WriteLine ("GetQuarterFirstDay(endDate2)={0}", GetQuarterFirstDay (endDate2));
Console.WriteLine ("GetQuarterFirstDay(endDate3)={0}", GetQuarterFirstDay (endDate3));
if (DateTime.Compare (GetQuarterFirstDay (endDate2), GetQuarterFirstDay (startDate).AddMonths (24)) > 0)
Console.WriteLine ("> 2 Yeas");
else
Console.WriteLine ("<= 2 Yeas");
if (DateTime.Compare (GetQuarterFirstDay (endDate3), GetQuarterFirstDay (startDate).AddMonths (24)) > 0)
Console.WriteLine ("> 2 Yeas");
else
Console.WriteLine ("<= 2 Yeas");
}
}
}
produce
dt1=09.07.2009 00:00:00
dt2=09.08.2009 00:00:00
dt3=09.09.2009 00:00:00
Now we have=22.04.2010 11:21:45
endDate1=23.04.2012 11:21:45
endDate2=21.04.2012 11:21:45
endDate3=22.08.2012 11:21:45
GetQuarterFirstDay(startDate)=01.04.2010 00:00:00
GetQuarterFirstDay(endDate1)=01.04.2012 00:00:00
GetQuarterFirstDay(endDate2)=01.04.2012 00:00:00
GetQuarterFirstDay(endDate3)=01.07.2012 00:00:00
<= 2 Yeas
> 2 Yeas
EDITED: I fixed an error from the first version. Now it should works correct.

Categories

Resources