Finding a given day or weekday x months from today
-
Hi all, I've searched high and low and even found a few code project articles but nothing in C# and nothing that really does what I need which is to write some code to find a particular day in a month given an occurrence (which could be first, second, third, fourth or last) and a day type or name (which could be "weekday", "Monday", "Tuesday" ... "Sunday") and a number of months (1-12). I'm doing this as part of replicating the Outlook calendar recurrence dialog so users of my app can select custom dates to run reports. So, for example, they might want to run one report on the first weekday of each month, another report on the third Wednesday of every two months and maybe another one on the last Friday of each 3 months. My only starting position is that I have today's date which will always be the date the report has just run... from that I need to work out any combination of the above so I can then schedule the next occurrence of the report. I've tried doing it with some sort of formula but nothing I've tried works so at present I'm adding the months to today's date, setting the day to 1 then simply looping though testing each day until I find what I want - but it's pretty clumsy to be honest and getting the last occurrence is pretty tricky too. Are there folks out there that have better math brains than me that would know how to do this in a formula? All help and suggestions would be very welcome. Mike
-
Hi all, I've searched high and low and even found a few code project articles but nothing in C# and nothing that really does what I need which is to write some code to find a particular day in a month given an occurrence (which could be first, second, third, fourth or last) and a day type or name (which could be "weekday", "Monday", "Tuesday" ... "Sunday") and a number of months (1-12). I'm doing this as part of replicating the Outlook calendar recurrence dialog so users of my app can select custom dates to run reports. So, for example, they might want to run one report on the first weekday of each month, another report on the third Wednesday of every two months and maybe another one on the last Friday of each 3 months. My only starting position is that I have today's date which will always be the date the report has just run... from that I need to work out any combination of the above so I can then schedule the next occurrence of the report. I've tried doing it with some sort of formula but nothing I've tried works so at present I'm adding the months to today's date, setting the day to 1 then simply looping though testing each day until I find what I want - but it's pretty clumsy to be honest and getting the last occurrence is pretty tricky too. Are there folks out there that have better math brains than me that would know how to do this in a formula? All help and suggestions would be very welcome. Mike
Hi, you need the DateTime class. For any given date it allows you: - to move back to the first of the month (new DateTime(year, month, 1)) - to advance by a number of months (AddMonths) - to figure out the day of the week (DayOfWeek: 0=sunday,... 6=saturday) - to advance by a number of days (AddDays), possibly a number depending on the day of week you have and the day of week you want. That should suffice to solve whatever problem you have. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi all, I've searched high and low and even found a few code project articles but nothing in C# and nothing that really does what I need which is to write some code to find a particular day in a month given an occurrence (which could be first, second, third, fourth or last) and a day type or name (which could be "weekday", "Monday", "Tuesday" ... "Sunday") and a number of months (1-12). I'm doing this as part of replicating the Outlook calendar recurrence dialog so users of my app can select custom dates to run reports. So, for example, they might want to run one report on the first weekday of each month, another report on the third Wednesday of every two months and maybe another one on the last Friday of each 3 months. My only starting position is that I have today's date which will always be the date the report has just run... from that I need to work out any combination of the above so I can then schedule the next occurrence of the report. I've tried doing it with some sort of formula but nothing I've tried works so at present I'm adding the months to today's date, setting the day to 1 then simply looping though testing each day until I find what I want - but it's pretty clumsy to be honest and getting the last occurrence is pretty tricky too. Are there folks out there that have better math brains than me that would know how to do this in a formula? All help and suggestions would be very welcome. Mike
First, you should check out the System.Globalization.Calendar class. It is a much richer tool than the simple DateTime class, and offers features like GetDaysInMonth(), GetWeekOfYear(), IsLeapYear(), etc. These can help in finding first and last, figuring out the weeks in a month, etc. You can use the info from Calendar to help you build a DateTime, and the rest could be done with some math.
-
Hi all, I've searched high and low and even found a few code project articles but nothing in C# and nothing that really does what I need which is to write some code to find a particular day in a month given an occurrence (which could be first, second, third, fourth or last) and a day type or name (which could be "weekday", "Monday", "Tuesday" ... "Sunday") and a number of months (1-12). I'm doing this as part of replicating the Outlook calendar recurrence dialog so users of my app can select custom dates to run reports. So, for example, they might want to run one report on the first weekday of each month, another report on the third Wednesday of every two months and maybe another one on the last Friday of each 3 months. My only starting position is that I have today's date which will always be the date the report has just run... from that I need to work out any combination of the above so I can then schedule the next occurrence of the report. I've tried doing it with some sort of formula but nothing I've tried works so at present I'm adding the months to today's date, setting the day to 1 then simply looping though testing each day until I find what I want - but it's pretty clumsy to be honest and getting the last occurrence is pretty tricky too. Are there folks out there that have better math brains than me that would know how to do this in a formula? All help and suggestions would be very welcome. Mike
Thanks for the input guys, it's appreciated. Luc, I have been using DateTime - you simply couldn't solve this without that class. Sorry if my post wasn't clear (though I thought I explained it pretty well) - I was looking more for a formula based on today, using DateTime to find out the day I wanted. Jon, Thanks for that suggestion - I didn't even think to use the Calendar class so will investigate that now. EDIT: I just looked at the Calendar class but you have to already know the day you want so it's not much use to me unfortunately. I do have it working quite nicely now via the loop method but, being a programmer, I just *know* there's a nicer, quicker, more efficient way to do it!
-
Thanks for the input guys, it's appreciated. Luc, I have been using DateTime - you simply couldn't solve this without that class. Sorry if my post wasn't clear (though I thought I explained it pretty well) - I was looking more for a formula based on today, using DateTime to find out the day I wanted. Jon, Thanks for that suggestion - I didn't even think to use the Calendar class so will investigate that now. EDIT: I just looked at the Calendar class but you have to already know the day you want so it's not much use to me unfortunately. I do have it working quite nicely now via the loop method but, being a programmer, I just *know* there's a nicer, quicker, more efficient way to do it!
No it wasn't sufficiently clear to me. Maybe a couple examples would help. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi all, I've searched high and low and even found a few code project articles but nothing in C# and nothing that really does what I need which is to write some code to find a particular day in a month given an occurrence (which could be first, second, third, fourth or last) and a day type or name (which could be "weekday", "Monday", "Tuesday" ... "Sunday") and a number of months (1-12). I'm doing this as part of replicating the Outlook calendar recurrence dialog so users of my app can select custom dates to run reports. So, for example, they might want to run one report on the first weekday of each month, another report on the third Wednesday of every two months and maybe another one on the last Friday of each 3 months. My only starting position is that I have today's date which will always be the date the report has just run... from that I need to work out any combination of the above so I can then schedule the next occurrence of the report. I've tried doing it with some sort of formula but nothing I've tried works so at present I'm adding the months to today's date, setting the day to 1 then simply looping though testing each day until I find what I want - but it's pretty clumsy to be honest and getting the last occurrence is pretty tricky too. Are there folks out there that have better math brains than me that would know how to do this in a formula? All help and suggestions would be very welcome. Mike
nzmike wrote:
at present I'm adding the months to today's date, setting the day to 1
Same as i would do ...
nzmike wrote:
then simply looping though testing each day until I find what I want
... not what i would do. :) Let's say you find the 1st of the month of interest is a Wed. (Apr.2009) (which most datetime/cal classes will give). From where you are now, do: enum {Mon, Tues, Wed, Thurs, Fri, Sat, Sun} Days; int weekfirst[7] = [6, 7, 1, 2, 3, 4, 5] int weeklast[7] = [27, 28, 29, 30, 24, 25, 26] int *week = weekfirst; int mul = 1; If parse 'last' then mul = -1 week = weeklast int weekdayidx = (index of lowest number in first 5 elements of week) int occurence = (default = 0, parse 'first' gives 0, 'second' gives 1, ...); int offset = 7 * occurence * mul; int day = offset; switch( type ) { case Weekday : a bit more fidly, but should be easy from here; break; case Monday : day += week[Mon]; break; ... } e.g. 2nd Thurs = 7*1*1 + 2 = 9th 1st Mon = 7*0*1 + 6 = 6th last Tues = 7*0*-1 + 28 = 28th 2nd last Sat = 7*1*-1 + 25 = 18th [EDIT] whoops, sorry about the C syntax, forgot is was in .net forum [/EDIT]
...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack
-
Thanks for the input guys, it's appreciated. Luc, I have been using DateTime - you simply couldn't solve this without that class. Sorry if my post wasn't clear (though I thought I explained it pretty well) - I was looking more for a formula based on today, using DateTime to find out the day I wanted. Jon, Thanks for that suggestion - I didn't even think to use the Calendar class so will investigate that now. EDIT: I just looked at the Calendar class but you have to already know the day you want so it's not much use to me unfortunately. I do have it working quite nicely now via the loop method but, being a programmer, I just *know* there's a nicer, quicker, more efficient way to do it!
Well, you need to start with something. You already mentioned that you were starting with the current day. From there, Calendar gives you some useful tools to determine important things, like if the current year is a leap year (in which the last day of Feb. is the 29th), etc. Calendar won't do the work for you...it will just simplify the work. There is indeed an algorithm that needs to be written to calculate the series, and that I'll leave up to you. I just thought the Calendar class might help you with some of the odd cases (i.e. leap year), finding the last day of each month dynamically, etc.